-1

I'm using JFileChooser which don't want to close after I press close button. The problem is that after I press the close button, it opens again 3+ times, and finally closes.

My code:

javaButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     JFileChooser fileChooser = new JFileChooser();
     fileChooser.setDialogTitle("Save");

     int option = fileChooser.showSaveDialog(null);
     if (option == JFileChooser.APPROVE_OPTION) {
         String filename = fileChooser.getFileFilter().getDescription();
            try {
                ChartUtilities.saveChartAsPNG(new File(filename), chart, getWidth(), getHeight());
                } catch (java.io.IOException exc) {
                System.err.println("Error writing image to file"); 
                }
     }
     if (option == JFileChooser.CANCEL_OPTION) {
                 System.out.println("Task canceled!");
                 //tried: fileChooser.setVisible(false); // >> same problem

     }
   }
});

Any advice?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 3
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please use a consistent and logical indent for code blocks. – Andrew Thompson Nov 16 '12 at 13:10
  • `if (option == JFileChooser.CANCEL_OPTION) {` should probably be just `else {`. – Andrew Thompson Nov 16 '12 at 13:13
  • This may or may not be your issue, but placing your closing braces `}` on the same line as your code blocks (see catch block), or indenting them to the same position as the open brace `{` (see try block) is very confusing, and could easily lead to logic errors. Instead, you may find it easier (and we will definitely understand your code more clearly) to place `}` on a line by itself, lined up with the start of the block (the character `t` in `try`, for instance) you're intending to close. – dimo414 Nov 16 '12 at 13:19

3 Answers3

2

Any option you choose in the JFileChooser closes the dialog if the selection is valid.

However, please note that the code under if (option == JFileChooser.CANCEL_OPTION) will never execute because you are already inside a branch that evaluated option == JFileChooser.APPROVE_OPTION to true.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • The brackets are closed well now, but nothing changed. Sorry for my mistake, when I putted the code in the code question. – Apopei Andrei Ionut Nov 16 '12 at 13:28
  • 3
    *"Sorry for my mistake,"* Rather than offer apologies, I'd prefer to see you act on my first advice (both points). As it is, you have had 4 or more people chasing their tails over code that was not easily readable, and possibly does not even contain the problem code. – Andrew Thompson Nov 16 '12 at 13:33
0

My suggestion would be to specify the parent of the JFileChooser and not setting it to null. What is the parent of that dialog? Is it a JFrame?

Have a look at this simple example and i m sure it will work for you.

http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

nikkatsa
  • 1,751
  • 4
  • 26
  • 43
0

Try this:

javaButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                 JFileChooser fileChooser = new JFileChooser();
                 fileChooser.setDialogTitle("Save");

                 int option = fileChooser.showSaveDialog(null);
                 if (option == JFileChooser.APPROVE_OPTION) {
                     String filename = fileChooser.getFileFilter().getDescription();
                        try {
                            ChartUtilities.saveChartAsPNG(new File(filename), chart, getWidth(), getHeight());
                            } catch (java.io.IOException exc) {
                            System.err.println("Error writing image to file"); }
                 } // here.

                 if (option == JFileChooser.CANCEL_OPTION) {
                             System.out.println("Task canceled!");

                 }
        }}); // one more }
matheuslf
  • 309
  • 1
  • 9