lucasasecas
lucasasecas

Reputation: 964

How to make changes in a swing jframe from a thread

I'm working with swing in Java and in my program I have to make some calculations. These calculations take a lot of time so I decided to create a thread for these calculations and from the thread, change the gui.

sequence diagram

The problem is that when the thread completes the calculations, add information in a tab of a jtabbedPane but when I try to change the title of this tab it use the method setTitleAt and call updateUI() and the program throw the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthTabbedPaneUI.getFontMetrics(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateTabRects(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.calculateLayoutInfo(Unknown Source)
at javax.swing.plaf.synth.SynthTabbedPaneUI$2.calculateLayoutInfo(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at javax.swing.RepaintManager$2.run(Unknown Source)
at javax.swing.RepaintManager$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Is it posible that this error appears because the call it's maked by the thread?

Upvotes: 0

Views: 204

Answers (1)

Jose Ferrer
Jose Ferrer

Reputation: 1238

Regardless of the other problems you're having, you should not directly update the UI from something that is not the AWT event thread. If you have an asynchronous process (like your calculation thread) that needs to modify the UI, you need to queue that operation for the event thread to execute it. Use SwingUtilites.invokeLater() and pass it a Runnable that will make the necessary UI changes. I haven't tried this exact scenario, but I bet you can create the panel in the worker thread and use the Runnable to attach it to the tabbed pane.

Upvotes: 1

Related Questions