Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel not repainting automatically  RSS feed

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I have a JFrame with four JPanels in it. In one of the JPanels, I have a JLabel (in a JScrollPane) as the only component in that JPanel that I use for displaying Sprites (little objects). Everything works except for the fact that whenever I minimize this JFrame or maximize it, the Sprites disappear. Only when I move the JScrollPane associated with the JLabel do the Sprites reappear.
I know this is a problem with paintComponent() not being called. I tried everything I know but it still dosent work.
I tried setting listeners for the JFrame so that when an event occurs I call the method to repaint the Sprites. But it didnt work.
Your help would be greatly appreciated.
[ March 06, 2007: Message edited by: Shyam Prasad Murarka ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post a demo program, with just a panel/scrollpane/label (with paintComponent())
so we can see how you've put it together
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael Dunn,
I tried creating a sample program to recreate the problem that I was facing in my original program. But while doing so, I hit upon another problem (and strangely enough this problem is not there in the original program).

The code for SpriteImage class:



The code for Sprite class:



The Exception that I am getting is this:

[code]
Exception in thread "main" java.lang.NullPointerException
at Sprite.paintSprite(Sprite.java:7)
at SpriteFrame.paintSprites(SpriteFrame.java:37)
at SpriteFrame.setupGUI(SpriteFrame.java:32)
at SpriteFrame.<init>(SpriteFrame.java:16)
at SpriteFrame.main(SpriteFrame.java:11)
[code]

I can see that the Graphics object returned upon calling label.getGraphics() is returning a null object. I do not understand why this is happening because in my original program, no null object is returned. And I am very sure that this code is not much different from the original program.
Therefore, I fail to understand this issue.
[ March 08, 2007: Message edited by: Shyam Prasad Murarka ]
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand why this is happening because in my original program, no null object is returned. And I am very sure that this code is not much different from the original program.

That little difference surely caused the problem.
label.getGraphics() will return null if the label is currently not displayable.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a demo program of the problem of using getGraphics()
reply #8

http://forum.java.sun.com/thread.jspa?threadID=5141360

run the program, see what happens
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Yes, thank you very much. I see that I called the paintSprite() method before calling the setVisible() method.
Ok, so that apart, in my original program, I have this JLabel inside a JPanel.
Now, I have added ComponentListener to the JFrame as well as the JLabel so that when the frame loses focus or its size is altered or the JScrollPane bars are moved, I can repaint the Sprites.
But the problem is that when the JFrame IS minimized or loses focus, the Sprites do not appear BUT if the bars of JScrollPane is moved the Sprited DO appear.
I DO call the paintSprites() method when any of the methods of the ComponentListener interface is invoked.
I know a workaround this is to subclass JLabel and modify the paintComponent() method but I want to avoid that.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
The reason I want to avoid subclassing JLabel is that I need the JLabel to display a background Map and this Map determines the size of the JLabel which is wrapped with a JScrollPane.
Now, if I try using the paintComponent() method of JLabel, then I will have to paint the background Map also there. As a result, suppose my Map is larger than the display area, then the rest of the Map will not be viewable because the JScrollPane will not be aware that there are contents outside the display area.
The JScrollPane will allow us to scroll to other parts of the JLabel only if we use setText() or setIcon() methods.
I really hope you are getting my point because my explaination itself looks very confusing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!