bryanberg
asked on
java program to browse files on windows for specific file type, then save path
Hello
Im trying to write a java program which will browse files on windows for a specific file type and then save the path
Ive done something like this using jsp
Thank you
thank you very much
Bryan
Im trying to write a java program which will browse files on windows for a specific file type and then save the path
Ive done something like this using jsp
Thank you
thank you very much
Bryan
Or are you bent on making your own file-chooser ;-) ?
What do you mean by "browse"? If you mean like swing's JFileChooser, then just use previous posts (don't try to reinvent the wheel)!
If you mean more like MSWindows search, then maybe something like this:
String pattern;
FileFilter filter=new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().matches (pattern);
}
};
public List SearchFiles(String pattern, File start)
{
if (pattern!=null) {
this.pattern=pattern;
}
File[] dirContents=start.listFile s(filter);
List contents=new ArrayList(dirContents.leng th);
for (int i = 0; i < dirContents.length; i++) {
File eachFile=dirContents[i];
if (eachFile.isDirectory()) {
contents.addAll(SearchFile s(null, eachFile);
} else
contents.add(eachFile);
}
return dirContents;
}
If you mean more like MSWindows search, then maybe something like this:
String pattern;
FileFilter filter=new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().matches
}
};
public List SearchFiles(String pattern, File start)
{
if (pattern!=null) {
this.pattern=pattern;
}
File[] dirContents=start.listFile
List contents=new ArrayList(dirContents.leng
for (int i = 0; i < dirContents.length; i++) {
File eachFile=dirContents[i];
if (eachFile.isDirectory()) {
contents.addAll(SearchFile
} else
contents.add(eachFile);
}
return dirContents;
}
ASKER
I just want a button on a form that when you click it a file open window comes up with only .sqt files showing
Im trying to add this functionality to an existing program which already does stuff when a button is clicked.
I can change the jpg/gif filter to sqt once the code works but now im still getting an error
Im trying to use the snippet from
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html
I get an error from the line:
int returnVal = chooser.showOpenDialog(par ent);
Im guessing Im supposed to change parent to something else but im not sure what
Ive seen it as myWindow, and tried that, also tried using Hello.this, since thats the name of the class
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
//import ExampleFileFilter;
public class hello
{
public static void main( String[] args ) {
try{
//Process DTA = Runtime.getRuntime().exec( "C:\\DTASe lect\\DTAS elect.bat" );
//final Process DTA = Runtime.getRuntime().exec( "java -cp c:\\DTASelect DTASelect");
//Process p = Runtime.getRuntime().exec( "dir");
//Process p = Runtime.getRuntime().exec( );
//javax.swing.JTextArea textArea;
//java.io.InputStream in = DTA.getInputStream();
//java.io.BufferedReader reader = new java.io.InputStreamReader( in); // wrapping the stream
//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
//textArea.append(r + "\r\n");
JFrame frame = new JFrame( "HelloJava" );
JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
JButton button = new JButton("DTA");
button.setSize( 50, 50 );
frame.setDefaultCloseOpera tion( JFrame.EXIT_ON_CLOSE );
final JTextArea area = new JTextArea( );
area.setText("Howdy\n");
button.addActionListener(n ew ActionListener( ) {
public void actionPerformed(ActionEven t ae) {
try{
final Process DTA = Runtime.getRuntime().exec( "java -cp c:\\DTASelect DTASelect");
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg") ;
filter.addExtension("gif") ;
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filt er);
//ERROR IN LINE BELOW WITH HELLO.THIS
int returnVal = chooser.showOpenDialog(hel lo.this);
if(returnVal == JFileChooser.APPROVE_OPTIO N) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile(). getName()) ;
}
}
catch(Exception e) {
System.out.println("no good");
}
} });
//java.io.BufferedReader reader = new java.io.InputStreamReader( in); // wrapping the stream
//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);
//javax.swing.JTextArea textArea;
//textArea.append("hello") ;
Container c = frame.getContentPane( );
c.add( label,BorderLayout.NORTH);
c.add( area,BorderLayout.CENTER);
c.add( button,BorderLayout.SOUTH) ;
frame.setSize( 300, 300 );
frame.setVisible( true );
}
catch(Exception e) {
System.out.println("no good");
}
}}
Im trying to add this functionality to an existing program which already does stuff when a button is clicked.
I can change the jpg/gif filter to sqt once the code works but now im still getting an error
Im trying to use the snippet from
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html
I get an error from the line:
int returnVal = chooser.showOpenDialog(par
Im guessing Im supposed to change parent to something else but im not sure what
Ive seen it as myWindow, and tried that, also tried using Hello.this, since thats the name of the class
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
//import ExampleFileFilter;
public class hello
{
public static void main( String[] args ) {
try{
//Process DTA = Runtime.getRuntime().exec(
//final Process DTA = Runtime.getRuntime().exec(
//Process p = Runtime.getRuntime().exec(
//Process p = Runtime.getRuntime().exec(
//javax.swing.JTextArea textArea;
//java.io.InputStream in = DTA.getInputStream();
//java.io.BufferedReader reader = new java.io.InputStreamReader(
//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
//textArea.append(r + "\r\n");
JFrame frame = new JFrame( "HelloJava" );
JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
JButton button = new JButton("DTA");
button.setSize( 50, 50 );
frame.setDefaultCloseOpera
final JTextArea area = new JTextArea( );
area.setText("Howdy\n");
button.addActionListener(n
public void actionPerformed(ActionEven
try{
final Process DTA = Runtime.getRuntime().exec(
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg")
filter.addExtension("gif")
filter.setDescription("JPG
chooser.setFileFilter(filt
//ERROR IN LINE BELOW WITH HELLO.THIS
int returnVal = chooser.showOpenDialog(hel
if(returnVal == JFileChooser.APPROVE_OPTIO
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().
}
}
catch(Exception e) {
System.out.println("no good");
}
} });
//java.io.BufferedReader reader = new java.io.InputStreamReader(
//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);
//javax.swing.JTextArea textArea;
//textArea.append("hello")
Container c = frame.getContentPane( );
c.add( label,BorderLayout.NORTH);
c.add( area,BorderLayout.CENTER);
c.add( button,BorderLayout.SOUTH)
frame.setSize( 300, 300 );
frame.setVisible( true );
}
catch(Exception e) {
System.out.println("no good");
}
}}
ASKER
o yeah and thanks for help
: >
: >
whats the error?
>> int returnVal = chooser.showOpenDialog(hel lo.this);
Use: int returnVal = chooser.showOpenDialog ( frame ) ;
Use: int returnVal = chooser.showOpenDialog ( frame ) ;
ASKER
I changed the value to frame and now it compiles !
C:\j2sdk1.4.2_05\bin\javac -classpath C:\j2sdk1.4.2_05\dem
o\jfc\FileChooserDemo\File ChooserDem o.jar hello.java
but when i try and run the java program i still ge the error:
C:\DTASelect\test_run>C:\j 2sdk1.4.2_ 05
Exception in thread "main" java.lang.NoClassDefFoundE rror: ExampleFileFilter
at hello.main(hello.java:43).
ExampleFileFilter class is in the jar file FileChooserDemo.Jar
do i need to do another import statement ?
below is my code:
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
//import FileChooserDemo.*;
public class hello
{
public static void main( String[] args ) {
try{
//Process DTA = Runtime.getRuntime().exec( "C:\\DTASe lect\\DTAS elect.bat" );
//final Process DTA = Runtime.getRuntime().exec( "java -cp c:\\DTASelect DTASelect");
//Process p = Runtime.getRuntime().exec( "dir");
//Process p = Runtime.getRuntime().exec( );
//javax.swing.JTextArea textArea;
//java.io.InputStream in = DTA.getInputStream();
//java.io.BufferedReader reader = new java.io.InputStreamReader( in); // wrapping the stream
//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
//textArea.append(r + "\r\n");
final JFrame frame = new JFrame( "HelloJava" );
JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
JButton button = new JButton("DTA");
button.setSize( 50, 50 );
frame.setDefaultCloseOpera tion( JFrame.EXIT_ON_CLOSE );
final JTextArea area = new JTextArea( );
area.setText("Howdy\n");
button.addActionListener(n ew ActionListener( ) {
public void actionPerformed(ActionEven t ae) {
try{
final Process DTA = Runtime.getRuntime().exec( "java -cp c:\\DTASelect DTASelect");
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg") ;
filter.addExtension("gif") ;
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filt er);
//int returnVal = chooser.showOpenDialog(hel lo.this);
int returnVal = chooser.showOpenDialog ( frame ) ;
if(returnVal == JFileChooser.APPROVE_OPTIO N) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile(). getName()) ;
}
}
catch(Exception e) {
System.out.println("no good");
}
} });
//java.io.BufferedReader reader = new java.io.InputStreamReader( in); // wrapping the stream
//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);
//javax.swing.JTextArea textArea;
//textArea.append("hello") ;
Container c = frame.getContentPane( );
c.add( label,BorderLayout.NORTH);
c.add( area,BorderLayout.CENTER);
c.add( button,BorderLayout.SOUTH) ;
frame.setSize( 300, 300 );
frame.setVisible( true );
}
catch(Exception e) {
System.out.println("no good");
}
}}
C:\j2sdk1.4.2_05\bin\javac
o\jfc\FileChooserDemo\File
but when i try and run the java program i still ge the error:
C:\DTASelect\test_run>C:\j
Exception in thread "main" java.lang.NoClassDefFoundE
at hello.main(hello.java:43).
ExampleFileFilter class is in the jar file FileChooserDemo.Jar
do i need to do another import statement ?
below is my code:
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
//import FileChooserDemo.*;
public class hello
{
public static void main( String[] args ) {
try{
//Process DTA = Runtime.getRuntime().exec(
//final Process DTA = Runtime.getRuntime().exec(
//Process p = Runtime.getRuntime().exec(
//Process p = Runtime.getRuntime().exec(
//javax.swing.JTextArea textArea;
//java.io.InputStream in = DTA.getInputStream();
//java.io.BufferedReader reader = new java.io.InputStreamReader(
//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
//textArea.append(r + "\r\n");
final JFrame frame = new JFrame( "HelloJava" );
JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
JButton button = new JButton("DTA");
button.setSize( 50, 50 );
frame.setDefaultCloseOpera
final JTextArea area = new JTextArea( );
area.setText("Howdy\n");
button.addActionListener(n
public void actionPerformed(ActionEven
try{
final Process DTA = Runtime.getRuntime().exec(
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg")
filter.addExtension("gif")
filter.setDescription("JPG
chooser.setFileFilter(filt
//int returnVal = chooser.showOpenDialog(hel
int returnVal = chooser.showOpenDialog ( frame ) ;
if(returnVal == JFileChooser.APPROVE_OPTIO
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().
}
}
catch(Exception e) {
System.out.println("no good");
}
} });
//java.io.BufferedReader reader = new java.io.InputStreamReader(
//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);
//javax.swing.JTextArea textArea;
//textArea.append("hello")
Container c = frame.getContentPane( );
c.add( label,BorderLayout.NORTH);
c.add( area,BorderLayout.CENTER);
c.add( button,BorderLayout.SOUTH)
frame.setSize( 300, 300 );
frame.setVisible( true );
}
catch(Exception e) {
System.out.println("no good");
}
}}
what was the command u used to run it, it appears it got truncated above
ASKER
ok so what ive got works ... : )
C:\DTASelect\test_run>java -classpath C:\j2sdk1.4.2_05\demo\jfc\ FileChoose rDemo\
FileChooserDemo.jar;C:\DTA Select\tes t_run hello
as long as i include the paths to both the jar file AND the hello.class file
when i try using the set classpath line the examplefilefilter is not found
C:\DTASelect\test_run>set CLASSPATH = %CLASSPATH%;.;C:\j2sdk1.4. 2_05\demo\ jfc\Fi
leChooserDemo\FileChooserD emo.jar;.; %cd%
maybe im typing something wrong
any ideas as to how to find what value the classpath currently holds ? and/or reset it to be the way its supposed to be
needless to say
im almost there !
all's i really need now is to return the path of the file selected
ive changed the files selected to .sqt
and
the program returns the pathname including the file selected:
System.out.println("You chose to open this file: " +
chooser.getSelectedFile(). getPath()) ;
i just don't want the filename in there
but im almost there & ^ )
thanks !
b
C:\DTASelect\test_run>java
FileChooserDemo.jar;C:\DTA
as long as i include the paths to both the jar file AND the hello.class file
when i try using the set classpath line the examplefilefilter is not found
C:\DTASelect\test_run>set CLASSPATH = %CLASSPATH%;.;C:\j2sdk1.4.
leChooserDemo\FileChooserD
maybe im typing something wrong
any ideas as to how to find what value the classpath currently holds ? and/or reset it to be the way its supposed to be
needless to say
im almost there !
all's i really need now is to return the path of the file selected
ive changed the files selected to .sqt
and
the program returns the pathname including the file selected:
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().
i just don't want the filename in there
but im almost there & ^ )
thanks !
b
> any ideas as to how to find what value the classpath currently holds ?
echo %CLASSPATH%
or
set CLASSPATH
> i just don't want the filename in there
if you just want the parent directory, use the File's getParent() method.
echo %CLASSPATH%
or
set CLASSPATH
> i just don't want the filename in there
if you just want the parent directory, use the File's getParent() method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
http://www.javaalmanac.com/cgi-bin/search/find.pl?words=jfilechooser