Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually position JComponent inside JPanel

I want to programmatically move my JLabel to a specific location inside my JPanel. I have tried setLocation(int x, int y), but it doesn't work. I am trying to not use any layout manager.

like image 361
oletus Avatar asked May 12 '10 16:05

oletus


People also ask

Is a JPanel a JComponent?

JPanel is a subclass of the JComponent, which is a child of the Container; therefore, JPanel is also considered a container. There are many methods used by JPanel, which it inherits from its parent classes.

How do I change the layout of a JPanel in Java?

The most common JPanel constructor has no parameters. This creates a panel with the default layout (FlowLayout). Call setLayout() to change the layout. JPanel p = new JPanel(); p.

How do you use JComponent?

To use JComponent, the procedure is usually as follows: create a subclass of JComponent; override the paintComponent() method to draw whatever graphics are required; override getPreferredSize(), getMinimumSize() and getMaximumSize() to make your new component "behave" properly.


1 Answers

Here is a great tutorial on how to layout your components without using a layout manager.

http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html

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 149
jjnguy Avatar answered Oct 27 '22 21:10

jjnguy