-2

Trying to terminate a swing gui, start the same gui and terminate it.

I am using the answer to this question but it seem to only work once.

The code below cycles once and hangs after printing 2.

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class Hello {
    void createAndShowGUI() {
        frame=new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label=new JLabel("Hello World");
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }
    void goodbye() {
        WindowEvent wev=new WindowEvent(frame,WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
    }
    static void helloGoodbye() throws InterruptedException,InvocationTargetException {
        System.out.println("enter");
        final Hello hello=new Hello();
        System.out.println("1");
        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                System.out.println("3");
                hello.createAndShowGUI();
                System.out.println("4");
            }
            {
                System.out.println("2");
            }
        });
        System.out.println("5");
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                hello.goodbye();
                System.out.println("6");
            }
        });
        // Thread.sleep(1000);
        System.out.println("exit");
    }
    public static void main(String[] args) throws InvocationTargetException,InterruptedException {
        for(int i=0;i<10;i++)
            helloGoodbye();
        System.out.println("exiting main");
    }
    JFrame frame;
}

Why doesn't it work?

Community
  • 1
  • 1
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 3
    Chit, your code doesn't even compile. But also important, what is your goal. Per your question, you sound as if you want to start, stop, then start and stop a Swing GUI repeatedly? Why? What UI experience are you driving at? – Hovercraft Full Of Eels Feb 23 '14 at 06:46
  • it compile fine for me in eclipse. i was using a jdk 8. i will add a final. each invocation will save a picture of the gui in a file. – Ray Tayek Feb 23 '14 at 06:47
  • You're missing a `final` access modifier on your Hello variable declaration, so no, it shouldn't compile. – Hovercraft Full Of Eels Feb 23 '14 at 06:50
  • It cannot compile without `final` modifier before `Hello`.. Since it's in an *inner* class... – Maroun Feb 23 '14 at 06:51
  • Where is there any file IO or image code of any manner? – Hovercraft Full Of Eels Feb 23 '14 at 06:52
  • this is just a sample that hangs. – Ray Tayek Feb 23 '14 at 06:53
  • Then to repeat, `"But also important, what is your goal. Per your question, you sound as if you want to start, stop, then start and stop a Swing GUI repeatedly? Why? What UI experience are you driving at?"`. Seriously. – Hovercraft Full Of Eels Feb 23 '14 at 06:58
  • Actually, the error hint in Kepler is... "Cannot refer to a non-final variable Hello inside an inner class defined in a different method." It asks me if I'd like to add `final`, so... – ChiefTwoPencils Feb 23 '14 at 06:59
  • 2
    @BobbyDigital: you're chiming in about 10 minutes late. We've moved on from this and on to bigger and better issues. Right now at least I'm trying to figure out what the heck the OP is driving at, what they're actual question is. – Hovercraft Full Of Eels Feb 23 '14 at 07:00
  • @HovercraftFullOfEels - great; what's your point? – ChiefTwoPencils Feb 23 '14 at 07:01
  • @BobbyDigital: my point is that he's already fixed that error as per previous comments. Move on. – Hovercraft Full Of Eels Feb 23 '14 at 07:03
  • @HovercraftFullOfEels - Perhaps you cannot fathom someone stepping away from "precious" for a moment and neglecting to see the additional commentary. Please excuse me, I do apologize, to you and your two pals, for allowing the most important thing in my life to slip for, what, ten minutes. I promise it won't *ever* happen again. – ChiefTwoPencils Feb 23 '14 at 07:05
  • 2
    @BobbyDigital: I stepped on a nerve. I do likewise apologize. – Hovercraft Full Of Eels Feb 23 '14 at 07:36

1 Answers1

4

The program doesn't hang but rather it exits because:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From the doc:

EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

Here, it seems like DISPOSE_ON_CLOSE will result in the behavior you are expecting. I also do not really see a reason to be posting this window closing event when you can call setVisible(false) and dispose. Without making assumptions about what the program is actually intended to do, both of these options will result in all 10 frames being shown/hidden/disposed.

I would guess that the reason the program halts after printing the '2' is that calling invokeAndWait causes any previously queued events to be pumped, including the window closing event.

Radiodef
  • 37,180
  • 14
  • 90
  • 125