• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel-Retaining the contents of the JPanel

 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

When ever the window is resized or minimised the JPanel becomes empty. I know that the repaint() method is called and the panel gets repainted. I wanted to know how can we avoid this so that the contents are retained. I hope someone puts up the code for that.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mohammed sanaullah:
When ever the window is resized or minimised the JPanel becomes empty. I know that the repaint() method is called and the panel gets repainted. I wanted to know how can we avoid this so that the contents are retained. I hope someone puts up the code for that.


Actually, that someone should be you. Without your code we don't know exactly what you are doing, and can only guess at an appropriate answer. With your code, we can give you a much more precise reply. Having said that, here's my guess:

You're likely painting in this JPanel but not doing it from within a method that overrides paintComponent. This is the method that the JVM calls whenever the OS (or your program) asks for the GUI to be repainted.

Here is a sample program that shows a picture in a JPanel. To try this, you will need to change this line:

private final static String FILE_NAME =

to match a file that is accessible to your program. I must warn you, though that I am no expert in Graphics, so this may not be the only or best way to do this, but it works for me. Any constructive criticism is most welcome.


[ January 13, 2008: Message edited by: pete steinbear ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!