• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting a Glass Pane for JPanel

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic