Crazy Coder
Crazy Coder

Reputation: 23

Componenets alignment in GridBagLayout

I am using GridBagLayout to align components. Actually, i have two buttons which i want to align like this:

Desired layout:

https://i.sstatic.net/4yHu4.jpg

But the following code results in the following layout:

Resulted layout:

https://i.sstatic.net/PtCUt.png

My code:

    iconAdd = new ImageIcon(getClass().getResource("../images/add.png"));
    add = new JButton(iconAdd);
    add.setPreferredSize(new Dimension(130, 100));
    add.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true));
    add.setCursor(Cursor.getPredefinedCursor(12));
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(5, 5, 5, 5);
    pane.add(add, gbc);

    iconSearch = new 
    ImageIcon(getClass().getResource("../images/search.png"));
    search = new JButton(iconSearch);
    search.setCursor(Cursor.getPredefinedCursor(12));
    search.setPreferredSize(new Dimension(130, 100));
    search.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true));
    gbc.gridx++;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(5, 5, 5, 5);
    pane.add(search, gbc);

Any help would be highly appreciated.

Upvotes: 0

Views: 144

Answers (2)

camickr
camickr

Reputation: 324207

gbc.weightx = 1;

You are telling the layout to give extra space to each component. So essentially each component becomes half the size of the frame.

You really only want that constraint set for the second button, so it takes up all the remaining space.

Read the section from the Swing tutorial on How to Use GridBagLayout which explains how the weightx/y constraints should be used.

Also, an easier solution would be do just use a FlowLayout. Create a panel with a FlowLayout. Add the buttons to the panel. Then add the panel to the BorderLayout.PAGE_START of the frame.

Upvotes: 1

c0der
c0der

Reputation: 18812

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagLayoutDemo extends JFrame{

    GridBagLayoutDemo(){

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0};
        gridBagLayout.columnWidths = new int[]{0,0,300};
        getContentPane().setLayout(gridBagLayout);

        JButton button1 = new JButton("Long Button");
        GridBagConstraints c1 = new GridBagConstraints();
        c1.fill = GridBagConstraints.HORIZONTAL;
        c1.weightx = 0.0;
        c1.gridwidth = 3;
        c1.gridx = 0;
        c1.gridy = 0;
        getContentPane().add(button1, c1);

        JButton button2 = new JButton("Button 2");
        GridBagConstraints c2 = new GridBagConstraints();
        c2.weightx = 0.5;
        c2.gridx = 0;
        c2.gridy = 1;
        getContentPane().add(button2, c2);

        JButton button3 = new JButton("Button 3");
        GridBagConstraints c3 = new GridBagConstraints();
        c3.weightx = 0.5;
        c3.gridx = 1;
        c3.gridy = 1;
        getContentPane().add(button3, c3);

        pack();
        setVisible(true);
    }


    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutDemo();
            }
        });
    }
}

enter image description here

Upvotes: 1

Related Questions