I have a JFrame which uses BorderLayout as a layout manager.
I also have 2 panels on this JFrame, at CENTER and SOUTH position.
The panel on CENTER position is changed dynamically on some user actions (actually when user press “Next” on the bottom panel).
The code is follwing:
private void switchToPanelByState(State state)
{
this.getContentPane().removeAll();
JPanel panel = _panels.get(state);
this.getContentPane().add(panel, BorderLayout.CENTER);
this.getContentPane().add(_controlPanel, BorderLayout.SOUTH);
this.pack();
}
The problem is that after a few changes of panels the parts of “old” (from previous panels) GUI elements are appears on the current panel.
And also from time to time i see the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)
However I don’t know if it’s related to the problem.
What is the possible source of these problems?
Any advise would be appreciated.
BorderLayout accepts only one JComponent in the central
Area, then there is no reason toremove()/ removeAll(); your code should be as shown below:or directly
2) nobody know what’s parameter
stateis, then without SSCCE isn’t possible answering this question3) I can simulating RepaintManager’s Exceptions, when CustomPainting is faster than latency from Native OS