This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

repaint problem in JPanel

Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done some drawing in JPanel using graphics object of JPanel
JFrame frame = JFrame();
JPanel panel = JPanel();
//add some components in panel

//get Graphics object of panel
Graphics g = panel.getGraphics();
//do some drwaing in 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.
Ranch Hand
Posts: 1143
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

If you haven't already read it, I suggest you peruse this article:
Hope this has helped you.
Good Luck,
yashendra chandrakar
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, i followed the way u specified, its working fine now.
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic