Alptugay
Alptugay

Reputation: 1686

Java GridBag Layout not working properly

I have two tables and two buttons. I want table-1 to be in the first column and as wide as 3 units, then I want the two buttons to the right of the table much more narrower (1 units in width), I want button one on the top and button2 on the bottom. Now to the right of these buttons I want the other table (table-2) again as wide as 3 units. To achieve this I have used the following constraints:

        //table1
        c = new GridBagConstraints(0, 0, 3, 2, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        c.fill = GridBagConstraints.BOTH;
        outerPanel.add(all_app_scrollpane, c);

        //button1
        c = new GridBagConstraints(3, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        outerPanel.add(addButton, c);

        //button2
        c = new GridBagConstraints(3, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        outerPanel.add(removeButton, c);

        //table2
        c = new GridBagConstraints(4, 0, 3, 2, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        c.fill = GridBagConstraints.BOTH;
        outerPanel.add(curr_app_scrollpane, c);

Let's look at the GridBagCOnstraints of table1. As far as I know, the first parameter states that it will start from the 0. column and will be 3 columns wide (third parameter). And then button1 will start from the third column and will be one column wide. But I think I know something wrong as the result is very different then I expect.

enter image description here

I want these buttons much more narrower and tables much wider. How can I do that? I want to accomplish this using GridBag, as this small panel is just a small part of a very complex gui, and the whole gui is designed using gridbag. So I don't want to change the coding style.

Upvotes: 3

Views: 734

Answers (2)

itshorty
itshorty

Reputation: 1562

You have to set the grow settings (weightx) of the layout so that only the left and right column are growing.

Like this:

    contentPane.setLayout(new GridBagLayout());
    ((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0, 0};
    ((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 10, 0, 0, 0};
    ((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0, 1.0E-4};
    ((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};

    //---- leftBtn ----
    leftBtn.setText("left Button");
    contentPane.add(leftBtn, new GridBagConstraints(0, 0, 1, 5, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));

    //---- rightBtn ----
    rightBtn.setText("right Button");
    contentPane.add(rightBtn, new GridBagConstraints(2, 0, 1, 5, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));

    //---- addBtn ----
    addBtn.setText("add Button");
    contentPane.add(addBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));

    //---- remBtn ----
    remBtn.setText("rem Button");
    contentPane.add(remBtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0), 0, 0));

So it gives that: demo

Greetings Florian

Edit: Here a Screenshot from the IDE, with additional space between the buttons: demo2

Upvotes: 3

Guillaume Polet
Guillaume Polet

Reputation: 47637

On button1 and button2, reduce their weightx to 0.0. Something like this:

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Test4 {

    protected void initUI() {
        JFrame frame = new JFrame("test");
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.5;
        gbc.weighty = 1.0;
        gbc.gridheight = 2;
        panel.add(getTable(), gbc);
        gbc.gridx = 2;
        panel.add(getTable(), gbc);

        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.fill = GridBagConstraints.NONE;
        gbc2.anchor = GridBagConstraints.CENTER;
        gbc2.gridx = 1;
        gbc2.weighty = 1.0;
        panel.add(new JButton("Button 1"), gbc2);
        gbc2.gridy = 1;
        panel.add(new JButton("Button 2"), gbc2);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    private Component getTable() {
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 10; i++) {
            Vector<String> row = new Vector<String>();
            for (int j = 0; j < 3; j++) {
                row.add("Cell (" + i + "," + j + ")");
            }
            data.add(row);
        }
        Vector<String> columns = new Vector<String>();
        columns.add("Column 1");
        columns.add("Column 2");
        columns.add("Column 3");
        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scroll;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test4().initUI();
            }
        });
    }
}

Sorry for the constraint reuse, it is bad for readability.

Upvotes: 3

Related Questions