• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to: clear the JPanel before repainting?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 30
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Marshal
Posts: 80962
526
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80962
526
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80962
526
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic