tags:

views:

547

answers:

4

Is it possible to add a JLabel on top of another JLabel? Thanks.

+1  A: 

The short answer is yes, as a JLabel is a Container, so it can accept a Component (a JLabel is a subclass of Component) to add into the JLabel by using the add method:

JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);

In the above code, the insideLabel is added to the outsideLabel.

However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.

So, the question comes down what one really wants to accomplish by adding a label on top of another label.


Edit:

From the comments:

well, what i wanted to do was first, read a certain fraction from a file, then display that fraction in a jlabel. what i thought of was to divide the fraction into 3 parts, then use a label for each of the three. then second, i want to be able to drag the fraction, so i thought i could use another jlabel, and place the 3'mini jlabels' over the big jlabel. i don't know if this will work though..:|

It sounds like one should look into how to use layout managers in Java.

A good place to start would be Using Layout Managers and A Visual Guide to Layout Managers, both from The Java Tutorials.

It sounds like a GridLayout could be one option to accomplish the task.

JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));

In the above example, the JPanel is made to use a GridLayout as the layout manager, and is told to make a row of JLabels.

coobird
oh. cool thanks! im not really familiar with these so i asked here. :)
annaoj
well, what i wanted to do was first, read a certain fraction from a file, then display that fraction in a jlabel. what i thought of was to divide the fraction into 3 parts, then use a label for each of the three. then second, i want to be able to drag the fraction, so i thought i could use another jlabel, and place the 3'mini jlabels' over the big jlabel. i don't know if this will work though..:|
annaoj
sounds like this is going to work..thanks, coobird! :)
annaoj
A: 

it's a matter of layout. you can do that using null layout (with hard coded locations) or with a custom layout.

Omry
I've been trying to do this, but it doesn't work for me. :c maybe i'm doing something wrong. i'll try again. thanks Omry!
annaoj
you can use the list() function to get a nice dump of the current container hierarchy, should help you debug your problem.
Omry
great! thanks! should i encounter problems again, can i post it here again?
annaoj
if it's about the same question, sure.if your current question is solved, accept one of the answers and if you have another question just create a new one.
Omry
A: 

The answer to your original question is yes for the reasons given that any Component can be added to a Container.

The reason you don't see the second label is because by default a JLabel uses a null layout manager and the size of the second label is (0, 0) so there is nothing to paint. So all you need to do is set the bounds of the second label and away you go.

You can't use a layout manager if you want to drag components around because as soon as you resize the frame etc, the layout manager will be invoked and the components will be repositioned based on the layout manager of the component.

camickr
oh. but is it possible to have a label on top of another, and be able to drag both labels as one? :|
annaoj
If the mouse listener is added to the parent label, then all the children will also be dragged when it is moved.
camickr
oh..but a friend said that it's not possible.. said i should use a panel. wah. now im lost. :|
annaoj
It is possible and I already told you how to do it. Its 4 lines of code, 2 to create the labels one to set the bounds of the child label and the 4th to add the child label to the parent label. It should take about 5 minutes to test it. Try it and post your SSCCE if it doesn't work. If you don't know what a SSCCE is then search the web.
camickr
A: 

you can use a JLayeredPane and set it's border to No Border.

Saher
I'm sorry, that's an easy way if you use a designer! Like the one in Netbeans.
Saher