• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting a Glass Pane for JPanel  RSS feed

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I know we can set Glass Panes for containers like JFrame but why can't we do the same for JPanel.
I have a Map Zone in a JPanel and I wanted "objects" to be placed on an image that is already there in the JPanel. The idea that occured to me was to set a glass pane on the JPanel and then place those "objects" there. But it seems we are not allowed to add glass panes for JPanel.
Is there any workaround this?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use JPanel's paintComponent() to draw your image, then add objects to the panel

example in the Swing FAQ
http://faq.javaranch.com/view?BackgroundImageOnJPanel
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Michael,
Thanks for your reply. But, I also wanted to set an alpha channel to these objects.
Do you think it will be a good idea, if I set a glass pane for the main JFrame and then work in only that section of the glass pane that overlays the MapZone?
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shyam Prasad Murarka:
I know we can set Glass Panes for containers like JFrame but why can't we do the same for JPanel.


It sounds like you want to use a JRootPane instead of a JPanel.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Brian,
Oh.. so JRootPane can be added to a JFrame?
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Out of many classes, I have got two classes namely; MapZone (subclass of JRootPane) and SpriteLayer (subclass of JPanel).
Now, I set the glass pane of MapZone as SpriteLayer. I now do the following:


Everything works fine if I call the setVisible() method of SpriteLayer from that class itself.
But if I remove the comments from the above code fragment, then the glass pane does NOT appear.
It seems that we cannot call this method from outside the class. Why is this so?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!