Ben Hagel
Ben Hagel

Reputation: 441

Calling repaint() For JPanels

When I repaint() the panel, where I'm placing my JLabels on, I get errors, but it still works. I tried putting the panel.repaint(); code in a try statement and that didn't help. Anyone else having problems with repaint() for JPanels?

Error message (copied from the comments):

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 21 
at java.awt.Container.getComponent(Unknown Source) 
at javax.swing.JComponent.rectangleIsObscured(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) )

Upvotes: 0

Views: 558

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

Make sure that all modifications to the UI are carried out in the Event Dispatching Thread, this includes adding and removing components from containers.

As Leon states, using the invokeLater method will ensure that operations are carried on the EDT correctly.

Upvotes: 1

Leon
Leon

Reputation: 988

I'm not really sure but I think I stumbled upon the same problem before. I believed using SwingUtilities.invokeLater helped me backed then. Give it a try, I'm not just exactly sure.

Upvotes: 2

Related Questions