Link to home
Start Free TrialLog in
Avatar of ycomp
ycomp

asked on

JFileChooser doesn't terminate

I've got a problem with JFileChooser.

For some reason the thread doesn't quit. It stays active in my IDE (Eclipse) until I terminate it. Anyone know what's going on? Doesn't matter if you choose a file or cancel, it still doesn't terminate.

package test;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class TestChooser {
 
  public static void main(String[] args) {
   
    String theFile;
   
    // Get the name of the file
    if (args.length < 1) {
      JFileChooser chooser = new JFileChooser();
      File theDir = new File("C:\\");
      chooser.setCurrentDirectory(theDir);
      JFrame frame = new JFrame("chooser");
      int returnVal = chooser.showOpenDialog(frame);
      if(returnVal == JFileChooser.APPROVE_OPTION)
        theFile = chooser.getSelectedFile().getPath();
      else {
        System.out.println("You did not choose a file!");
        return;
      }
    } else theFile = args[0];

    System.out.println("The file selected was: " + theFile);
   
  }
}

btw. Does anyone know how to add a filter to it? I only want to choose *.doc Microsoft Word files.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Nothing obvious wrong. Try frame.dispose() at the end.

There's a filter example at the top of the API docs for JFileChooser

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFileChooser.html
I'll give you a quick tip you can use this example :

// A method that requires what you need :

  private void open()
  {
   JFileChooser x = new JFileChooser();
   File fileName = new File();

   x.addChoosableFileFilter(new Filter1());

   x.setDialogTitle("Open . . .");
   x.setFileSelectionMode(JFileChooser.FILES_ONLY);
   x.setCurrentDirectory(new File("C:/"));

   int result = x.showOpenDialog(null);

   if (result == JFileChooser.CANCEL_OPTION)
   return;

   fileName = x.getSelectedFile();

   if (fileName == null || fileName.getName().equals(""))
   {
   JOptionPane.showMessageDialog( this,"Select file name . . .",
   "Warning . . .", JOptionPane.WARNING_MESSAGE );
   }
   else
   {
   System.out.println("The file selected was: " + fileName);
   }
  }

// A File filter that filters a doc file :

import java.io.*;

public class Filter1 extends javax.swing.filechooser.FileFilter {

  public boolean accept (File file) {

  return file.isDirectory() || file.getAbsolutePath().endsWith(".doc");
  }

  public String getDescription () {

  return "Text files  \"*. doc\"";
  }
}

Hope that helps . . .
Javatm
Try my format and that might solve your problem =-)
Avatar of DrWarezz
DrWarezz

User System.exit(#); to exit when you want. And replace the '#' with a number to return, typically, use 0 to declare succes, or -1 for an error, etc.

>" Does anyone know how to add a filter to it? I only want to choose *.doc Microsoft Word files."
use the:  chooser.setFileFilter();   method to add a File Filter :)  Searh at java.sun.com for more info on that.

Hope that helps  :)
Best of luck,
[r.D]
Woops, ignore my post... I started writing it before any other posts.. :o\  Use theirs :P lol

gL,
[r.D]
Try this:

/*
 * TestChooser.java
 *
 */

package test;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class TestChooser {
   
    private String theFile;
   
    public TestChooser() {
      JFileChooser chooser = new JFileChooser();
      File theDir = new File("C:\\");
      chooser.setCurrentDirectory(theDir);
      chooser.addChoosableFileFilter(new MyFilter());
      JFrame frame = new JFrame("chooser");
      int returnVal = chooser.showOpenDialog(frame);
      if (returnVal == JFileChooser.APPROVE_OPTION)
        theFile = chooser.getSelectedFile().getPath();
      else
        System.out.println("You did not choose a file!");
    }
   
    public String getFile() {
        return theFile;
    }
 
    public static void main(String[] args) {
   
      String chosenFile = null;
   
    // Get the name of the file                      
    if (args.length < 1) {
        TestChooser tc = new TestChooser();
        chosenFile = tc.getFile();
    }
    else
        chosenFile = args[0];

    System.out.println("The file selected was: " + chosenFile);
  }
 
  public class MyFilter extends javax.swing.filechooser.FileFilter {
        public boolean accept(File file) {
            if (file.isDirectory()) return true;
            String filename = file.getName();
            return filename.endsWith(".doc");
        }
        public String getDescription() {
            return "*.doc (Word documents)";
        }
    }
 
}
Well that is not the correct thing to do. I gave the answers but you converted it.
Besides we are here to do help not to give-out all the answers ?
Avatar of ycomp

ASKER

zz, thanks the filter works great but it still has my "not terminating" problem.

Dr, System.exit(0) still doesn't terminate it.

CEHJ, frame.dispose() doesn't help.

Javatm, I tried your code (had to modify it a bit... I got a few error message in the IDE like there is no File() constructor) but anyhow I got it working. If cancel is chosen then it terminates fine. But if a file is selected there is the same problem... the app doesn't terminate.
What would you really like to do open the whole file or just display a message :
System.out.println("The file selected was: " + fileName);  ???
>> zz, thanks the filter works great but it still has my "not terminating" problem.
I don't have it at all
Oh I see, replace the constructor in my code with:

    public Downloader() {
        initComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // <<<<<<<<<<<<<<
    }
Make that

     setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

;°)
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to terminate the program after displaying the file change my some few codes above to :

   if (fileName == null || fileName.getName().equals(""))
   {
   JOptionPane.showMessageDialog( this,"Select file name . . .",
   "Warning . . .", JOptionPane.WARNING_MESSAGE );
   }
   else
   {
   System.out.println("The file selected was: " + fileName);
   System.exit(0);
   }
  }
It's as easy like that. Even in your own code try adding:

 JFrame frame = new JFrame("chooser");
 frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);    // <<<<<<<<<<<<<<<< Add this
>> Anyone know what's going on?
You have to say that the JFrame has to exit when it is closed
Avatar of ycomp

ASKER

javatm: of course I'm going to do something with the file after it was selected. But the code I posted was just the problem part of my actual code (to make it easier to work with). I like to isolate things to the core problem.
>> You have to say that the JFrame has to exit when it is closed
You have to say that the app has to exit when the frame is closed

Really bedtime ;°)
Avatar of ycomp

ASKER

cool zzynx, thanks. I got it to work now with the EXIT_ON_CLOSE
Nice to hear
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually if you make it as a method on what I showed U earlier you dont need to add a close action to the filechooser =-)
Avatar of ycomp

ASKER

zz, I'm using DISPOSE_ON_CLOSE now because I don't want to quit the app just yet (need to process the file I selected). I assume EXIT_ON_CLOSE will close the app. also I have to call frame.dispose afterwards. So everything's working great now.
>> So everything's working great now.
All right! :°)
Avatar of ycomp

ASKER

javatm: I tried your new code. it works great also. Thanks. Your old code was not terminating but maybe that was just because of the sloppy edits I made to get it to compile.
Any questions left?
Avatar of ycomp

ASKER

no zz, it just takes some time for me to run all the code and see that it works.
Thanks for accepting
>> it just takes some time for me to run all the code and see that it works
Of course! No worries :)
http://freewebs.com/cube-j

=-) Always glad to help . . .
Javatm