2

I have a problem with a Frame that I want to restart after an operation. In specific, below the start of the Frame:

public static void startHome() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new HomeGUI();
                frame.setVisible(true); 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Below the Frame:

public HomeGUI() throws IOException, InstantiationException, IllegalAccessException {
        setIconImage(Toolkit.getDefaultToolkit().getImage(ico_path));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 669, 516);
// etc...

Below my actual code for the restart, but doesn't work:

frame.dispose();
startHome();

Processing stops before to enter in run() method. I tried to set frame = null before invokeLater and after dispose(), but doesn't work.

Thank you to all

Angel
  • 31
  • 8
  • Your HomeGUI class must extend JFrame. Can you provide code of your HomeGUI class. – Aniket Thakur Jul 24 '13 at 12:50
  • when you call the dispose method, you probably saw that the Frame was closed.. the dispose close the frame and stop the process of the thread (the run method), so there's nothing at the object that holds Frame (if you wrote Frame1 f = new Frame1(), so f is then garbage).. you have to create a new instance after the dispose – Elior Jul 24 '13 at 12:51
  • @Elior [Top-Level Containers never will be GC'ed](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime) – mKorbel Jul 24 '13 at 14:36
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jul 24 '13 at 15:23

1 Answers1

1
  1. there are two ways (without any intentions to restart whatever) to (re)use

    • setIconImage()

    • CardLayout

  2. be sure setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to terminate current JVM

  3. proper LayoutManager, JComponent(s) must returns PreferredSize back to JFrame, then call

    • JFrame.pack()
    • JFrame.setLocation()
    • JFrame.setVisisble(true)
    • as last code lines in constructor
    • because I can't found any reason to use AbsoluteLayour e.g. setBounds(100, 100, 669, 516);
  4. dispose() or setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); is the same as setVisible(false), you can get all Top-Level Containers from current JVM by iterating in arrays of Window[] wins = Window.getWindows();

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I don't understand the effective changes to be applied for my code... Point 2: it's already used; Point 3: where I must insert this lines? Point 4: I don't understand... – Angel Jul 27 '13 at 08:11
  • whats real reason for "restart". to `Point 3: where I must insert this lines?` ---> last code lines after all GUI is created, `Point 4: I don't understand see my comment` follows link in my comment to `Elior Top-Level Containers never will be GC'ed` – mKorbel Jul 27 '13 at 12:31