-2

I have a few shutdown steps which need to execute during a WindowClosing event before being disposed. Everything is executing correctly, but I'd like to add the capability to provide shutdown status messages in an existing JLabel within the closing JFrame. Is it possible to update the JLabel text during a WindowClosing event?

Steve
  • 101
  • 5
  • See [this answer](https://stackoverflow.com/a/7073630/418556) for a description of how to affect the closing behavior of a `JFrame`. – Andrew Thompson Dec 19 '17 at 06:56
  • I do not see where that answer addresses making updates to the JFrame components while in the Windowclosing event. – Steve Dec 20 '17 at 02:54

3 Answers3

0

Sure. Just make sure component is not disposed before you start interacting with it.

SwingUtilities.invokeLater(new Runnable() {
     public void run() {
            // update label here
     }
});
yu.pitomets
  • 1,660
  • 2
  • 17
  • 44
0

but I'd like to add the capability to provide shutdown status messages in an existing JLabel within the closing JFrame

label.setText(....);
label.paintImmediately(label.getBounds());

The code in the listener executes on the Event Dispatch Thread so the GUI can't repaint itself until all the listener code is executed and by that time the GUI will be closed.

The paintImmediately(...) will allow the component to bypass the RepaintManager and paint itself right away.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Your explanation makes sense, but I tried using paintImmediately() without any luck. The frame still doesn't update inside the closing event. – Steve Dec 20 '17 at 22:59
0

I used the following code to execute the shutdown steps in the background and then close the JFrame.

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {      
        SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
            @Override
            public Void doInBackground() {

                // shutdown steps go here and can update the JLabel text

                frame.dispose();
                return null;
            }
        };
        worker.execute();
    }
});
Steve
  • 101
  • 5