3

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();
            }
        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Makaroni
  • 880
  • 3
  • 15
  • 34

2 Answers2

3

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

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Thank you it worked! Now I have another problem, since GUI loads serial.dll each time, the MATLAB give the error: "Native Library C:\serial.dll already loaded in another classloader". Can you help me solve this also? – Makaroni Aug 17 '11 at 14:58
  • @Makaroni: "Native Library whatever.dll already loaded in another classloader." seems to have been answered several times on SO: http://stackoverflow.com/questions/3724421/native-library-already-loaded-in-another-classloader has an answer and links to several others. – millimoose Aug 17 '11 at 15:11
2

I would use:

 setVisible(false);

instead of dispose(). That way you can call setVisible(true) to make it reappear.

dacwe
  • 43,066
  • 12
  • 116
  • 140