Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i put a graphic behind another graphic icon?

 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually discussed on the Swing forum. Moving.
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can subclass JPanel(or any JComponent) and the you can override paintComponenet method, to paint whatever you like.
 
Rob Camick
Ranch Hand
Posts: 2534
9
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
That's a nice one Rob. I've never thought that I can add another component to a JLabel.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2534
9
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic