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.setCurrentDirector y(theDir);
JFrame frame = new JFrame("chooser");
int returnVal = chooser.showOpenDialog(fra me);
if(returnVal == JFileChooser.APPROVE_OPTIO N)
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.
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.setCurrentDirector
JFrame frame = new JFrame("chooser");
int returnVal = chooser.showOpenDialog(fra
if(returnVal == JFileChooser.APPROVE_OPTIO
theFile = chooser.getSelectedFile().
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.
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(n ew Filter1());
x.setDialogTitle("Open . . .");
x.setFileSelectionMode(JFi leChooser. 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.showMessageDia log( this,"Select file name . . .",
"Warning . . .", JOptionPane.WARNING_MESSAG E );
}
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.Fi leFilter {
public boolean accept (File file) {
return file.isDirectory() || file.getAbsolutePath().end sWith(".do c");
}
public String getDescription () {
return "Text files \"*. doc\"";
}
}
Hope that helps . . .
Javatm
// A method that requires what you need :
private void open()
{
JFileChooser x = new JFileChooser();
File fileName = new File();
x.addChoosableFileFilter(n
x.setDialogTitle("Open . . .");
x.setFileSelectionMode(JFi
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.showMessageDia
"Warning . . .", JOptionPane.WARNING_MESSAG
}
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.Fi
public boolean accept (File file) {
return file.isDirectory() || file.getAbsolutePath().end
}
public String getDescription () {
return "Text files \"*. doc\"";
}
}
Hope that helps . . .
Javatm
Try my format and that might solve your problem =-)
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]
>" 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]
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.setCurrentDirector y(theDir);
chooser.addChoosableFileFi lter(new MyFilter());
JFrame frame = new JFrame("chooser");
int returnVal = chooser.showOpenDialog(fra me);
if (returnVal == JFileChooser.APPROVE_OPTIO N)
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.Fi leFilter {
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)";
}
}
}
/*
* 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.setCurrentDirector
chooser.addChoosableFileFi
JFrame frame = new JFrame("chooser");
int returnVal = chooser.showOpenDialog(fra
if (returnVal == JFileChooser.APPROVE_OPTIO
theFile = chooser.getSelectedFile().
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.Fi
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 ?
Besides we are here to do help not to give-out all the answers ?
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.
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); ???
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
I don't have it at all
Oh I see, replace the constructor in my code with:
public Downloader() {
initComponents();
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ; // <<<<<<<<<<<<<<
}
public Downloader() {
initComponents();
setDefaultCloseOperation(J
}
Make that
setDefaultCloseOperation(j avax.swing .JFrame.EX IT_ON_CLOS E);
;°)
setDefaultCloseOperation(j
;°)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.showMessageDia log( this,"Select file name . . .",
"Warning . . .", JOptionPane.WARNING_MESSAG E );
}
else
{
System.out.println("The file selected was: " + fileName);
System.exit(0);
}
}
if (fileName == null || fileName.getName().equals(
{
JOptionPane.showMessageDia
"Warning . . .", JOptionPane.WARNING_MESSAG
}
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.setDefaultCloseOpera tion(javax .swing.JFr ame.EXIT_O N_CLOSE); // <<<<<<<<<<<<<<<< Add this
JFrame frame = new JFrame("chooser");
frame.setDefaultCloseOpera
>> Anyone know what's going on?
You have to say that the JFrame has to exit when it is closed
You have to say that the JFrame has to exit when it is closed
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 ;°)
You have to say that the app has to exit when the frame is closed
Really bedtime ;°)
ASKER
cool zzynx, thanks. I got it to work now with the EXIT_ON_CLOSE
Nice to hear
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 =-)
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! :°)
All right! :°)
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?
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 :)
Of course! No worries :)
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