How to locate JLabels to an absolute position on Java GUI

AhmetB - Google picture AhmetB - Google · May 15, 2009 · Viewed 13.2k times · Source

I have many JLabels (which includes ImageIcons) in a JPanel.

And this JPanel is only a panel on the GUI; there are lots of other panels.

I want to place labels to the exact pixel coordinates on their JPanel container.

How can I do that without using GroupLayout?

Answer

Michael Myers picture Michael Myers · May 15, 2009

See Doing Without a Layout Manager (Absolute Positioning) in the Java tutorials.

Creating a container without a layout manager involves the following steps.

  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.