user1750156
user1750156

Reputation: 35

Closing Jframe after a file is selected in JFileChooser

I want to make it so that my JFrame closes after a file is selected in JFileChooser. How should I do this? I tried using the dispose(); function but it doesn't apply to the actionPerformed listener. Any tips?

    public static void createWindow() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton inbutton = new JButton("Select Input File");
    inbutton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
          Test method = new Test();
          File selectedFile = fileChooser.getSelectedFile();
          String outFile = selectedFile.getParent() + "/baseball_out.txt";
          String inFile = selectedFile.getPath();
          method.baseballedit(inFile, outFile);
          //ADD CLOSING ACTION HERE//
        }
      }
    });
    frame.add(inbutton);
    frame.pack();
    frame.setVisible(true);

}

Upvotes: 0

Views: 1297

Answers (1)

Reimeus
Reimeus

Reputation: 159784

I tried using the dispose(); function but it doesn't apply to the actionPerformed listener.

dispose needs to be invoked on the JFrame rather than on the ActionListener. Make frame final and you can then call

frame.dispose();

Upvotes: 1

Related Questions