Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JFileChooser doesn't terminate

Posted on 2004-09-21
30
Medium Priority
?
560 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:ycomp
  • 13
  • 8
  • 6
  • +2
30 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12115168
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
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115280
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
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115307
Try my format and that might solve your problem =-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:DrWarezz
ID: 12115334
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]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12115347
Woops, ignore my post... I started writing it before any other posts.. :o\  Use theirs :P lol

gL,
[r.D]
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115360
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)";
        }
    }
 
}
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115434
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 ?
0
 

Author Comment

by:ycomp
ID: 12115489
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.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115561
What would you really like to do open the whole file or just display a message :
System.out.println("The file selected was: " + fileName);  ???
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115568
>> zz, thanks the filter works great but it still has my "not terminating" problem.
I don't have it at all
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115592
Oh I see, replace the constructor in my code with:

    public Downloader() {
        initComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // <<<<<<<<<<<<<<
    }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115601
Make that

     setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

;°)
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 12115629
Sorry time for my bed:

      JFrame frame = new JFrame("chooser");
      frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);    // <<<<<<<<<<<<<<<< Add this in the constructor
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115631
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);
   }
  }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115651
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115664
>> Anyone know what's going on?
You have to say that the JFrame has to exit when it is closed
0
 

Author Comment

by:ycomp
ID: 12115674
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115679
>> 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 ;°)
0
 

Author Comment

by:ycomp
ID: 12115700
cool zzynx, thanks. I got it to work now with the EXIT_ON_CLOSE
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115728
Nice to hear
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 1000 total points
ID: 12115749
I never thought that you where having a problem w/ the main frame =-( ? Confused.

Anyway this was my own version :

import java.io.*;
import javax.swing.*;

public class FileChooser {
 
  File fileName;  
   
    public FileChooser() {

    JFileChooser x = new JFileChooser();

    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( null,"Select file name . . .",
    "Warning . . .", JOptionPane.WARNING_MESSAGE );
    }
    else
    {
    System.out.println("The file selected was: " + fileName);
    System.exit(0);
    }
    }
 
    public class Filter1 extends javax.swing.filechooser.FileFilter {

    public boolean accept (File file) {
 
    return file.isDirectory() || file.getAbsolutePath().endsWith(".doc");
    }

    public String getDescription () {

    return "Doc files  \"*. doc\"";
    }
    }
}
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12115819
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 =-)
0
 

Author Comment

by:ycomp
ID: 12115845
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115876
>> So everything's working great now.
All right! :°)
0
 

Author Comment

by:ycomp
ID: 12115926
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115927
Any questions left?
0
 

Author Comment

by:ycomp
ID: 12115972
no zz, it just takes some time for me to run all the code and see that it works.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115990
Thanks for accepting
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12115996
>> it just takes some time for me to run all the code and see that it works
Of course! No worries :)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12116043
http://freewebs.com/cube-j

=-) Always glad to help . . .
Javatm
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question