Search code examples
javamatlabuser-interfacedispose

Closing Java GUI without System.exit


How can I close Java GUI without System.exit? When I use dispose() the GUI does close, but after that it won't start again? I am using GUI to automatically grab and write an image from camera to disk, so I need to repeat this action each time when I start GUI. Later, I want to connect this GUI to start in MATLAB.

public class MainWindow {
    int fs_c = 1;
    MainWindow() {
        JFrame main_f = new JFrame("GUI");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        main_f.getContentPane().add(tabPane1, "Center");

        //main_f.setSize(500, 620);
        main_f.pack();
        main_f.setVisible(true);
        commandVal = 0;

        while (true) {
            if (fs_c == 1) {
                commandVal = 5;
            }
            if (commandVal == 5 || commandVal == 6) {
                cImage.sendFrame(0);
                JFileChooser save_d = new JFileChooser();
                File saveFile = save_d.getSelectedFile();
                cImage.writeImage(saveFile + ".jpg");
                fs_c = 0;
                main_f.dispose();
            } else if (commandVal == -1) {
                stopCameraStuff();
            }
        }
    }
}

Solution

  • You should also break the while loop somehow so that the method can be completed and return to the caller.