J doe J doe - 9 months ago 81
Java Question

GridBagLayout Jbutton Position

This is what I have :

enter image description here

and this is what I want to have :

enter image description here

Here's a piece of my code

container.add(conteneur2 , BorderLayout.EAST);


conteneur2.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

JButton Bouton= new JButton("New remote site");

// Bouton 1

gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.LINE_START;


So what's the problem?

Answer Source

You need to set the anchor NORTH

gbc.anchor = GridBagConstraints.NORTH;

and the y weight to 1 means all the free space is distributed in y direction.

gbc.weighty = 1;