How can I make a component span multiple cells in a GridBagLayout
Asked Answered
L

2

12

I have to make this for school:

GUI

This is the code I have so far:

import javax.swing.*;
import java.awt.*;

public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbl);

    JLabel label;
    JButton button;
    JTextField textField;
    JTextArea textArea = new JTextArea(10, 20);

    gbc.weightx = 1;
    label = new JLabel("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 0;
    add(textField ,gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    add(textField, gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    add(label ,gbc);

    textField = new JTextField();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    add(textField, gbc);

    label = new JLabel("text");
    gbc.weightx = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    add(label ,gbc);



    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridwidth = 2;
    gbc.gridx = 1;
    gbc.gridy = 3;

    add(textArea, gbc);

    gbc.weightx = 1;
    button = new JButton("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 4;
    add(button ,gbc);

    gbc.weightx = 1;
    button = new JButton("text");
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 4;
    add(button ,gbc);



}
public static void main(String[] args){
  AddressBookGui1 frame = new AddressBookGui1();
  frame.setTitle("Address Book");
  frame.setSize(400, 300);
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true); 
}

}

This is the result

(I still need to deal with padding and insets. I've gotten those to work in a much simpler program so I think I have a handle on that stuff)

I've tried the GridBagLayout Oracle tutorial and I'm not sure what I'm doing wrong. Can someone help me make it look more like it is supposed to? Specifically to make the text fields and text area span over 2 cells.

Landmass answered 20/6, 2013 at 3:25 Comment(4)
Specifically to make the text fields and text area span over 2 cells - Why do then have to span two cells? In your picture you have 2 columns and 5 rows. The only thing you need to change is to make the button right justified. For that I guess you would need to play with the anchor. Check out the tutorial again.Vetavetch
I have 3 columns. The last button is in column 3.Landmass
This is the grid I'm looking to makeLandmass
That may be your interpretation of the assignment, but there is no need for 3 columns as I have already stated. You just need the button to be right aligned in the 2nd column. If the goal of the assignment is to create 3 columns and play with the gridwidth constraint then that is fine, but it is not necessary. I would guess your problem is the gridx=3 for the "text" button. Remember row/colums are 0 offset so you are trying to position the button in the 4th column, not the 3rd.Vetavetch
H
12

gbc.gridwidth is the parameter that allows a component to span more than one column. For example, if you have 3 columns and 4 rows and you want a label to occupy the complete top row, then you need to assign the first cell for the label. and set gbc.gridwidth = 3;

Holotype answered 12/7, 2016 at 5:16 Comment(0)
N
11

Few things I noticed about your code.

  • Do not use setSize() of JFrame. This will cause abnormal behavior. Instead, let the frame size itself according to the size of its components. If you want the frame to be bigger, adjust not the size of the frame but the components inside it. You can either setpreferredSize or override the getpreferredsize of the component if you really want to adjust is size since GridBagLayout is one of those layout managers that respects the preferredSize of the component. Use pack() to remove the unecessary space.

  • Do not extend a JFrame, make your UI class to have a main panel and add all the components there. provide a getter for that panel (e.g. getUI()) for extracting the UI of that class.

  • Always reinstantiate the GridBagConstraints object whenever it is going to be applied to another component. This way it is more readable.

  • Use insets to put padding around the component.

  • Do not reuse same reference to different components;

  • Use Initial Thread

  • This is not standard but it I find it really helpful when working with GridBagLayout, in setting the constraints of gbc, make it in alphabetical order.

To solve your problem, here is the modified code with the things I pointed out about applied.

public class AddressBook {

    private JPanel pnlMain;

    public AddressBook() {
        pnlMain = new JPanel();
        pnlMain.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JLabel lblName = new JLabel("Name");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(0, 10, 0, 0);
        gbc.weightx = 1;
        pnlMain.add(lblName, gbc);

        JTextField txtName = new JTextField();
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 3;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 0, 0, 10);
        gbc.weightx = 1;
        pnlMain.add(txtName, gbc);

        JLabel lblPhone = new JLabel("Phone");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(0, 10, 0, 0);
        gbc.weightx = 1;
        pnlMain.add(lblPhone, gbc);

        JTextField txtPhone = new JTextField();
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 3;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5, 0, 0, 10);
        gbc.weightx = 1;
        pnlMain.add(txtPhone, gbc);

        JLabel lblEmail = new JLabel("Email");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(0, 10, 0, 0);
        gbc.weightx = 1;
        pnlMain.add(lblEmail, gbc);

        JTextField txtEmail = new JTextField();
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 3;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.weightx = 1;
        gbc.insets = new Insets(5, 0, 0, 10);
        pnlMain.add(txtEmail, gbc);

        JLabel lblAddress = new JLabel("Address");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(0, 10, 0, 0);
        gbc.weightx = 1;
        pnlMain.add(lblAddress, gbc);

        JTextArea txtAreaAddress = new JTextArea(10, 20);
        JScrollPane pane = new JScrollPane(txtAreaAddress);
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridwidth = 3;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5, 0, 0, 10);
        gbc.weightx = 1;
        pnlMain.add(pane, gbc);

        JButton btnSave = new JButton("Save");
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(10, 10, 10, 0);
        gbc.weightx = 1;
        pnlMain.add(btnSave, gbc);

        JButton btnCancel = new JButton("Cancel");
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.EAST;
        gbc.gridwidth = 1;
        gbc.gridx = 3;
        gbc.gridy = 4;
        gbc.insets = new Insets(10, 0, 10, 10);
        gbc.weightx = 1;
        pnlMain.add(btnCancel, gbc);

    }

    public JPanel getUI(){
        return pnlMain;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Address Book");
                frame.getContentPane().add(new AddressBook().getUI());
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}
Naamana answered 20/6, 2013 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.