Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate JLabels to an absolute position on Java GUI

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?

like image 960
ahmet alp balkan Avatar asked May 15 '09 15:05

ahmet alp balkan


People also ask

How do I set absolute layout in Java?

For this go to app > res > layout > activity_main. xml file and change the Constraint Layout to Absolute Layout and add TextViews.

How do I center a JFrame?

By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.


2 Answers

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.
like image 104
Michael Myers Avatar answered Sep 19 '22 09:09

Michael Myers


Either

  • set a custom LayoutManager (Container.setLayout) on the panel that sets the exact positions you want or
  • set a null layout manager (myPanel.setLayout(null);) and set component positions externally (Component.setBounds).
like image 33
Tom Hawtin - tackline Avatar answered Sep 18 '22 09:09

Tom Hawtin - tackline