• 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

How do i put a graphic behind another graphic icon?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a bigger graphic that i want to put beind the smaller graphic icon as its background. The smaller icon should be centered inside the bigger graphic. How do i do this?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually discussed on the Swing forum. Moving.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can subclass JPanel(or any JComponent) and the you can override paintComponenet method, to paint whatever you like.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just add a label to a label. Something like:



Or if you want to get a litter fancier you can use a Compound Icon for the icon of your label. The compound icon will give you more control over the placement of the child icon. For even more flexibility you can use the Background Panel.
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a nice one Rob. I've never thought that I can add another component to a JLabel.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically a JLabel is still a JComponent and therefore a Container. However, I do feel that Sun should have shielded access to methods like setLayout, add (probably by shielding access to addImpl), remove and removeAll. It's a bit worrying how you can mangle components like a JTable by adding a JLabel to it directly, or a JSpinner by changing its layout manager.

I try to shield all my custom JComponents by throwing an UnsupportedOperationException from setLayout, addImpl, remove(int), remove(Component) and removeAll. I'll then call super.xxx when I need to use these methods myself.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:It's a bit worrying how you can mangle components like a JTable by adding a JLabel to it directly, or a JSpinner by changing its layout manager.



Then don't do it.

Thats what makes Swing so powerfull, the ability to customize almost anything. Of course with great power comes great responsibility.

I gave the OP multiple choices. Its up to them to understand the pro's and con's of each and make an informed decision.

This is a simple suggestion for a JLabel not a JTable. However, how do you think the editor works on a JTable? The editor is added to the table at the appropriate cell location. So even the designers of the JDK take advantage of adding child components to a parent container.
 
reply
    Bookmark Topic Watch Topic
  • New Topic