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
bryanbergAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Or are you bent on making your own file-chooser ;-) ?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

MogalManicCommented:
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.listFiles(filter);
      List contents=new ArrayList(dirContents.length);
      for (int i = 0; i < dirContents.length; i++) {
            File eachFile=dirContents[i];
            if (eachFile.isDirectory()) {
                  contents.addAll(SearchFiles(null, eachFile);
            } else
                  contents.add(eachFile);
      }
      return dirContents;
}
0
bryanbergAuthor Commented:
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(parent);

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:\\DTASelect\\DTASelect.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.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      final JTextArea area = new JTextArea( );
        area.setText("Howdy\n");

 
button.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent 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(filter);

//ERROR IN LINE BELOW WITH HELLO.THIS
    int returnVal = chooser.showOpenDialog(hello.this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       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");
    }

}}
0
bryanbergAuthor Commented:
o yeah and thanks for help

: >
0
objectsCommented:
whats the error?
0
Mayank SAssociate Director - Product EngineeringCommented:
>>  int returnVal = chooser.showOpenDialog(hello.this);

Use:  int returnVal = chooser.showOpenDialog ( frame ) ;
0
bryanbergAuthor Commented:
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\FileChooserDemo.jar hello.java

but when i try and run the java program i still ge the error:

C:\DTASelect\test_run>C:\j2sdk1.4.2_05
Exception in thread "main" java.lang.NoClassDefFoundError: 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:\\DTASelect\\DTASelect.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.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      final JTextArea area = new JTextArea( );
        area.setText("Howdy\n");

 
button.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent 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(filter);
    //int returnVal = chooser.showOpenDialog(hello.this);
    int returnVal = chooser.showOpenDialog ( frame ) ;

    if(returnVal == JFileChooser.APPROVE_OPTION) {
       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");
    }

}}

0
objectsCommented:
what was the command u used to run it, it appears it got truncated above
0
bryanbergAuthor Commented:
ok so what ive got works ...  : )


C:\DTASelect\test_run>java -classpath C:\j2sdk1.4.2_05\demo\jfc\FileChooserDemo\
FileChooserDemo.jar;C:\DTASelect\test_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\FileChooserDemo.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
0
objectsCommented:
> 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.
0
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.