File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How do i put a graphic behind another graphic icon? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do i put a graphic behind another graphic icon?" Watch "How do i put a graphic behind another graphic icon?" New topic
Author

How do i put a graphic behind another graphic icon?

Ana Suvari
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
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

Joined: Oct 13, 2005
Posts: 38467
    
  23
Usually discussed on the Swing forum. Moving.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
You can subclass JPanel(or any JComponent) and the you can override paintComponenet method, to paint whatever you like.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
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

Joined: Oct 21, 2008
Posts: 142
That's a nice one Rob. I've never thought that I can add another component to a JLabel.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19675
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i put a graphic behind another graphic icon?