• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refreshing Jpanel

 
Alfred Harre
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can I update/refresh contents of a JPanel. It keeps on drawing new objects and won't clear previous one.

Thanks a million for your help.


Regards
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are drawing in paintComponent use super.paintComponent(g);. This clears the drawing area for each new journey thru your paint code. If you're adding components then it's a different story.
 
Joe Chrz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to remove components from a JPanel and add new ones in their place. For example, when the application loads, I have a picture in this panel. When I click on a button, I want the picture to be removed and 2 child panels to go in its place. The button I have works, but I need to resize the window for the screen to refresh. Is there an easier way?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am assuming your are issues at the very least a panel.remove(component) command somewhere. If you aren't you should. Next, in order to tell Swing that things need to be redrawn you need to issue a repaint() command on the JPanel. That should do the trick.
 
Joe Chrz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to remove all the contents of a panel without specifying panel.remove(specificComponent)?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
removeAll(). The Java API is actually pretty handy for these types of questions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!