This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
Joined: Oct 21, 2008
That's a nice one Rob. I've never thought that I can add another component to a JLabel.
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 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.
subject: How do i put a graphic behind another graphic icon?