aspose file tools
The moose likes Swing / AWT / SWT and the fly likes repaint problem in JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "repaint problem in JPanel" Watch "repaint problem in JPanel" New topic
Author

repaint problem in JPanel

yashendra chandrakar
Greenhorn

Joined: Mar 22, 2002
Posts: 22
Hi,
I have done some drawing in JPanel using graphics object of JPanel
eg.
JFrame frame = JFrame();
JPanel panel = JPanel();
//add some components in panel
...
panel.add();
...

//get Graphics object of panel
Graphics g = panel.getGraphics();
//do some drwaing in panel
g.drawLine();
frame.getContentPane().add(panel);

This code is working as expected ,displaying all the drawings i done.
But the problem is that when this Frame or panel is hidden or minimized,all the drawings are removed due to repaint of panel.
How to avoid this repaint of JPanel.
Thanks.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1112

Hi Yashendra,
Permit me to offer an analogy:
It's a nice, sunny day. You get into your car and start the engine. Then you switch on the windscreen wipers for five minutes. Then you switch off your windscreen wipers. Suddenly it starts to rain. You stop your car. You jump out into the busy street and start shouting at all the people you see, "how can I make it stop raining?"
You need to work with java's painting mechanism. Each swing component (like "JPanel") implements the "paintComponent()" method. Java invoke's each component's "paintComponent()" method when it needs to repaint the screen. Asking java not to repaint the screen is like asking Mother Nature not to make it rain.
The usual way to do custom painting is to subclass the relevant component -- "JPanel" in your case -- and override its "paintComponent()" method. So create a class that extends "JPanel" and put your special painting code in the "paintComponent()" method.
Example:

If you haven't already read it, I suggest you peruse this article:
http://java.sun.com/products/jfc/tsc/articles/painting/index.html
Hope this has helped you.
Good Luck,
Avi.
yashendra chandrakar
Greenhorn

Joined: Mar 22, 2002
Posts: 22
Hi
Avi,
Thanks, i followed the way u specified, its working fine now.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: repaint problem in JPanel
 
Similar Threads
dynamically adding a JPanel in JFrame - not reflecting
Problem with custom painting and multiple JPanel
Repaint() in ActionPerformed.
override paintComponent in JFrame
Repaint my swing app