This is no doubt a simple question for expert ‘swingers’ but after some research I’ve not come up with anything.
I have code in this form:
public static void main(Strings[] args) {
final JFrame frame = new JFrame("Symbol Formatter");
final JButton button = new JButton("Begin");
final JFileChooser filechoose = new JFileChooser();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
filechoose.showOpenDialog(frame);
filename = filechoose.getSelectedFile();
doSomething(filename);
button.setEnabled(false);
frame.setVisible(false); //edit for Andrew Thompson
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.pack();
frame.setSize(250, 75);
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
I want to close the swing application once doSomething(filename) is completed. At the moment, the original frame remains where it is and the application only terminates when the frame is closed manually. How can I close once doSomething(filename) has run once.
I’m sure this is a simple question and just requires changing setDefaultCloseOperation however, the Jframe documentation hasn’t helped me much.
Also, is there a way to add a title to the showOpenDialog? So that is has something like “Choose library file” as the title
You could use:
frame.dispose()