0

δve got à strange issue in a Java app made with Netbeans. It´s a memory card game. The main class is a JFrame that holds à panel the same size. The Panel is subdivided with a Grid Layout with each cell holding à card, an other class designed with à little panel and à label. That´s for the Design. The source of the main class bears many functions for the game and the one which occurs when game is over, a third JDialog class, must also reset the whole board and there comes The Bug, when the function uses the method removeAll(). It does removeAll() but the result only appears on screen once the user resizes the JFrame. There must be an other method to refresh the panel automatically. Will I have to read the full JavaWiki to find my answer ?

Thank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Nov 29 '13 at 00:52

1 Answers1

3

Call revalidate() and then repaint() on the container after removing or adding components to it. If it's the JFrame's contentPane, then you'll need to call these methods on it.

JPanel panel = (JPanel) jFrame.getContentPane();
panel.removeAll();
panel.revalidate();
panel.repaint();
  • The revalidate() method tells the container's layout managers to re-layout all componentss contained.
  • The repaint() method sends a request to the repaint manager to repaint the container which should rid the window of "dirty" regions.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373