Ewen
Ewen

Reputation: 1028

Java - set the exact position of a JComponent

I need help with setting the x and y position of a JLabel on a JPanel, not North,South,West,East.

Thank you for your time.

Upvotes: 1

Views: 5478

Answers (1)

FThompson
FThompson

Reputation: 28707

To have a JPanel respect exact locations, you should set its layout to null. Then, you can use setLocation on any JComponent to set its location within that JPanel.

JPanel panel = new JPanel();
panel.setLayout(null);
JLabel label = new JLabel("text");
label.setLocation(50, 20);
panel.add(label);

However, note that there are several downsides of absolute position (mentioned in comments below), and that you should utilize a LayoutManager to position your components.

Upvotes: 3

Related Questions