6

I need an help for positioning a JPanel in a specific position into a Jframe.

I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a specific x,y position.

It's something like this:

public class Frame extends JFrame {

    public Frame(){

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(250, 250);
        this.setSize(300,300);
        this.setVisible(true);

        JPanel panel = new JPanel();
        this.add(panel);
        panel.setLocation(150, 150);
        panel.add(new JButton("Hello!")); // just to show JPanel

    }//Frame()
}//Frame

I don't need a LayoutManager for the position of the JPanel, because I need to put that JPanel in a specific position (like 150,150 of the example). But if I do panel.setLocation(150,150) like in the code above, nothing happens, and JPanel remain in the north-centre of the frame (also if I change x,y number instead of 150,150).

And it shows like this:

https://i.stack.imgur.com/hd8RY.png

alecarnevale
  • 93
  • 1
  • 3
  • 7
  • 2
    For absolute positioning, you need to use a null layout manager. For more information, see [Doing Without a Layout Manager (Absolute Positioning)](http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html). – mre Nov 25 '13 at 15:12
  • 1
    @Graffio31 absolute positioning is not recommended as far my knowledge – Suganthan Madhavan Pillai Nov 25 '13 at 15:41

3 Answers3

5

Of the frame's content panel, the layout manager by default is a BorderLayout (NWSE+C). Do:

this.getContentPanel().setLayout(null);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    creating containers with absolutely positioned containers can cause problems if the window containing the container is resized. http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html – Suganthan Madhavan Pillai Nov 25 '13 at 15:42
  • 1
    @Graffio31, `I need to put that JPanel in a specific position (like 150,150 of the example).` - that is no reason to use a null layout. Hardcoding values is a silly idea. There area always better ways to do this. For example you put an `EmptyBorder` around the panel. Anybody who suggests using a null layout really deservies a down vote. Using a null layout will cause so many problems especially for beginner programmers who don't understand how Swing works. – camickr Nov 25 '13 at 16:10
  • @Suganthan ok thank you! But in my case I have to do .setResizable(false) so absolutely positioned is possibile whitout problems :) – alecarnevale Nov 25 '13 at 16:15
  • 2
    @Graffio31, it is NOT the proper solution. I gave you a better alternative. Learn how to take advantage of Swing. Using layout manager is a powerful feature, whether you use resizing or not. – camickr Nov 25 '13 at 16:21
2

using setBounds(new Rectangle(96, 67, 98, 41)); you can do this... just see the example

/**
 * This method initializes this
 *
 * @return void
 */
private void initialize() {
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
}

/**
 * This method initializes jContentPane
 *
 * @return javax.swing.JPanel
 */
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jContentPane.add(getLoginButton(), null);
    }
    return jContentPane;
}

}

BaseZen
  • 8,650
  • 3
  • 35
  • 47
Konzern
  • 110
  • 10
1

A JFrame object uses borderlayout manager by default and JPanels use framelayout manager by default. If you want to use absolute positioning then you MUST use null layout because any other layout manager will use setLocation() and setSize() methods according to its own moods and not according to how YOU want it.

Summary: Use setLayout(null) and use null manager and then use setBounds(X,Y,width,height) method.

user3015246
  • 139
  • 1
  • 2
  • 9
  • -1 this bad suggestionwas made 20 minutes earlier. There is no need to clutter the forum with duplicate suggestions. – camickr Nov 25 '13 at 16:11