Reputation: 707
I have a JFileChooser that is launched by a button click which calls the ExportFileChooser.createAndShowGUI() method. It works fine accept for when I close the JFileChooser a new empty window titled ExportFileChooser opens, how can I correct this so it does not launch?
Here is the code:
package org.annotationRoi3D.io;
import java.io.*;
import java.awt.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* This creates a dialog window for exporting
* and importing XML files.
*/
public class ExportFileChooser extends JPanel {
private static final long serialVersionUID = 1L;
public static File ExportFile;
JFileChooser fcExport;
public ExportFileChooser() {
super(new BorderLayout());
fcExport = new JFileChooser();
int returnValExport = fcExport.showSaveDialog(ExportFileChooser.this);
if (returnValExport == JFileChooser.APPROVE_OPTION) {
ExportFile = fcExport.getSelectedFile();
org.annotationRoi3D.io.ExportXML.OutputXML();
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frameExport = new JFrame("FileChooserExport");
frameExport.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frameExport.add(new ExportFileChooser());
//Display the window.
frameExport.pack();
frameExport.setVisible(true);
}
}
Thank you
Upvotes: 1
Views: 496
Reputation: 707
Thanks for all the feedback, I have found the solution.
Upon button click I do not call CreateAndShowGUI() but now create a new instance of ExportFileCHooser like this:
ExportFileChooser exportWindow = new ExportFileChooser();
that seems to work perfectly
Upvotes: 0
Reputation: 691685
Well, that's what your code does: it creates a JFrame with "FileChooseExport"
as title, and makes it visible. Why does the code do that if you don't want to show a frame?
The code of the ActionListener of your button should simply be:
JFileChooser fcExport = new JFileChooser();
int returnValExport = fcExport.showSaveDialog(thePanelContainingTheButton);
if (returnValExport == JFileChooser.APPROVE_OPTION) {
...
}
You don't need another ExportFileChooser panel, placed in another JFrame made visible, just to open a JFileChooser. The javadoc of JFileChoose contains example usage, BTW.
Upvotes: 3