Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing a JLabel at a specific x,y coordinate on a JPanel

I'm trying to place a series of JLabels at specific X and Y coordinates on a JPanel (and set its height and width, too). No matter what I do, each label winds up immediately to the right of the previous label and has the exact same size as all of the others.

Right now, my Jpanel is in a Grid Layout. I've tried Absolute Layout (illegal argument exception results), Free Design (no labels appear), Flow Layout (everything just gets squeezed to the center), and a few others.

Not sure what I need to do to make this work. Can anyone help? Thanks!

JLabel lbl1 = new JLabel("label 1");
JLabel lbl2 = new JLabel("label 2");
JLabel lbl3 = new JLabel("label 3");
JLabel lbl4 = new JLabel("label 4");
JLabel lbl5 = new JLabel("label 5");

myPanel.add(lbl1);
myPanel.add(lbl2);
myPanel.add(lbl3);
myPanel.add(lbl4);
myPanel.add(lbl5);

lbl1.setLocation(27, 20);
lbl2.setLocation(123, 20);
lbl3.setLocation(273, 20);
lbl4.setLocation(363, 20);
lbl5.setLocation(453, 20);

lbl1.setSize(86, 14);
lbl2.setSize(140, 14);
lbl3.setSize(80, 14);
lbl4.setSize(80, 14);
lbl5.setSize(130, 14);
like image 239
AndroidDev Avatar asked Aug 25 '12 05:08

AndroidDev


People also ask

Can you add a JLabel to a JPanel?

Generally JLabel objects are added to JPanel objects.

How do you change the alignment of a label in Java?

A JLabel object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area.

How do you put a JLabel on top of another JLabel?

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.

What is the difference between JPanel and JLabel?

by giving the JLabel constructor the String argument that is the text to describe what's in there. A JPanel, on the other hand, is a Panel, a designated part of the GUI. Given that it is a distinct part, it is naturally a Container, and should thus be given the stuff.


2 Answers

You have to set your container's Layout to null:

myPanel.setLayout(null);

However is a good advise also to take a look at the Matisse Layout Manager, I guess it is called GroupLayout now. The main problem with absolute positioning is what happens when the window changes its size.

like image 79
gersonZaragocin Avatar answered Oct 09 '22 06:10

gersonZaragocin


  1. Set the container's layout manager to null by calling setLayout(null).

  2. Call the Component class's setbounds method for each of the container's children.

  3. Call the Component class's repaint method.

Note:

Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Refer this link: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

like image 28
Vinesh Avatar answered Oct 09 '22 08:10

Vinesh