An SO User
An SO User

Reputation: 24998

GridBagLayout does not line up the buttons properly

enter image description here

The following is my code used to connect a MS Access database to a Java program. The Next button does not line up with the start of JLabel ID:

public DisplayScreen(){

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        this.setLayout(new GridBagLayout());
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        GridBagConstraints gbc = new GridBagConstraints();
        Insets in = new Insets(2,2,2,2);
        gbc.insets = in;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(l1,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(f1,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(l2,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(f2,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(l3,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(f3,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(l4,gbc);

        gbc.gridx++;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        this.add(f4,gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(next,gbc);

        gbc.gridx++;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(last,gbc);

        gbc.gridx += 2;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(first,gbc);

        gbc.gridx += 2;
        gbc.gridheight = 1;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(prev,gbc);

        this.setResizable(false);
        this.pack();
        try{
            r = new Query().getResultSet();
            r.next();
            f1.setText(r.getString("studID"));
            f2.setText(r.getString("fName"));
            f3.setText(r.getString("lName"));
            f4.setText(r.getString("fee"));
        }catch(Exception e){
            e.printStackTrace();
        }

    }  

Please tell me what went wrong.

Upvotes: 1

Views: 321

Answers (2)

JB Nizet
JB Nizet

Reputation: 691785

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(next,gbc);

    gbc.gridx++;

should of course be

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(next,gbc);

    gbc.gridx += 2;

since you want it to start from the beginning of the row, and fill two columns.

Upvotes: 2

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

You start the second row with gridx = 1. Positions are numbered from 0!

Upvotes: 0

Related Questions