• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to: clear the JPanel before repainting?

 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to plot the data into JPanel periodically. But the problem is everytime the new data gets plotted, the last time plotted data remains drawn in the JPanel. Resulting an overlaped plotted data.
My question is how do you clear the JPanel before repainting?

Thank you so much.

This is the code I am talking about:

 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For clear JPanel this is my suggest.

JPanel panel = new JPanel();
//remove all components in panel.
panel.removeAll();
// refresh the panel.
panel.updateUI();
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first line of your paintComponent method should read
 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I put
super.paintComponent(g);
at the first line of my
paintComponent()
and it works.

Thank you guys.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Only too pleased to help. The statement super.paintComponent(g) seems to return the JPanel or whatever to its original state, then you can draw whatever you want on it.
 
carol sa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nacho Espinosa ,

while hunting for solution for my problem updating my Jpanel, I found you suggestion.... you made my day with your code suggestion, my problem solved in a second
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carol sa wrote:Nacho Espinosa ,

while hunting for solution for my problem updating my Jpanel, I found you suggestion.... . . .
I hope you aren't using that suggestion while repainting. It is very inefficient to remove and re-create all your components.

For repainting, super.paintComponent(g);

If you need to repopulate your panel, then the remove methods are useful. But why would you wish to change the displayed components?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!