JTextField not aligning in GridBagLayout

413 views Asked by At

I've just started working with GridBagLayout, and the image below is pretty self-explanatory of the problem, I need the first JTextField of the first 4 rows to stretch all the way to the JLabel on the left, just like the right ones.

enter image description here

The Component's grid widths, from top to bottom, are :

1, 1, 1, 1

1, 3

1, 1, 1, 1

1, 3

2, 2

2, 2

Code of the GridBagConstraints settings in the JFrame :

Also, why even if I set anchor to 'NORTH', all the components still sit aligned at the center of the JPanel?

    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(10, 10, 10, 10);

    labelNome = new JLabel("Nome:");
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelNome, gbc);

    tfNome = new JTextField();
    gbc.gridx = 1;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfNome, gbc);

    labelIdade = new JLabel("Idade :");
    gbc.ipadx = 0;
    gbc.gridx = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelIdade, gbc);

    tfIdade = new JTextField();
    gbc.gridx = 3;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfIdade, gbc);

    labelEndereco = new JLabel("Endereço :");
    gbc.ipadx = 50;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelEndereco, gbc);

    tfEndereco = new JTextField();
    gbc.ipadx = 50;
    gbc.gridx = 1;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfEndereco, gbc);

    labelFiliacao = new JLabel("Filiação :");
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelFiliacao, gbc);

    tfFiliacao = new JTextField();
    gbc.gridx = 1;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfFiliacao, gbc);

    labelTurma = new JLabel("Turma :");
    gbc.ipadx = 0;
    gbc.gridx = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelTurma, gbc);

    tfTurma = new JTextField();
    gbc.gridx = 3;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfTurma, gbc);

    labelDisciplina = new JLabel("Disciplina :");
    gbc.ipadx = 0;
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelDisciplina, gbc);

    tfDisciplina = new JTextField();
    gbc.gridwidth = 1;
    gbc.ipadx = 50;
    gbc.ipady = 0;
    gbc.gridx = 1;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfDisciplina, gbc);

    adicionaDisciplina = new JButton("Adicionar disciplina");
    gbc.gridwidth = 2;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.fill = GridBagConstraints.NONE;
    add(adicionaDisciplina, gbc);

    limparDisciplina = new JButton("Limpar lista de disciplinas");
    gbc.gridx = 2;
    add(limparDisciplina, gbc);

    botaoSalvar = new JButton("Salvar");
    gbc.gridx = 0;
    gbc.gridy = 5;
    add(botaoSalvar, gbc);

    botaoCancelar = new JButton("Cancelar");
    gbc.gridx = 2;
    add(botaoCancelar, gbc);
2

There are 2 answers

0
A.K. On
import java.awt.*;
import javax.swing.*;
class example extends JFrame
{
    public example()
    {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
setSize(800,800);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(10, 10, 10, 10);

    JLabel labelNome = new JLabel("Nome:");
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelNome, gbc);

    JTextField tfNome = new JTextField();
    gbc.gridx = 1;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfNome, gbc);

    JLabel labelIdade = new JLabel("Idade :");
    gbc.ipadx = 0;
    gbc.gridx = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelIdade, gbc);

    JTextField tfIdade = new JTextField();
    gbc.gridx = 3;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfIdade, gbc);

    JLabel labelEndereco = new JLabel("Endereço :");
    gbc.ipadx = 50;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelEndereco, gbc);

    JTextField tfEndereco = new JTextField();
    gbc.ipadx = 50;
    gbc.gridx = 1;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfEndereco, gbc);

    JLabel labelFiliacao = new JLabel("Filiação :");
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelFiliacao, gbc);

    JTextField tfFiliacao = new JTextField();
    gbc.gridx = 1;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfFiliacao, gbc);

    JLabel labelTurma = new JLabel("Turma :");
    gbc.ipadx = 0;
    gbc.gridx = 2;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelTurma, gbc);

    JTextField tfTurma = new JTextField();
    gbc.gridx = 3;
    gbc.ipadx = 50;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfTurma, gbc);

    JLabel labelDisciplina = new JLabel("Disciplina :");
    gbc.ipadx = 0;
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    add(labelDisciplina, gbc);

    JTextField tfDisciplina = new JTextField();
    gbc.gridwidth = 1;
    gbc.ipadx = 50;
    gbc.ipady = 0;
    gbc.gridx = 1;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    add(tfDisciplina, gbc);

    JButton adicionaDisciplina = new JButton("Adicionar disciplina");
    gbc.gridwidth = 2;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.fill = GridBagConstraints.NONE;
    add(adicionaDisciplina, gbc);

    JButton limparDisciplina = new JButton("Limpar lista de disciplinas");
    gbc.gridx = 2;
    add(limparDisciplina, gbc);

    JButton botaoSalvar = new JButton("Salvar");
    gbc.gridx = 0;
    gbc.gridy = 5;
    add(botaoSalvar, gbc);

    JButton botaoCancelar = new JButton("Cancelar");
    gbc.gridx = 2;
    add(botaoCancelar, gbc);

    setVisible(true);
    }
    public static void main(String aaa[]){new example();}
    }

use this code

0
Amber On

GridBagLayout sizes the columns based on the preferred sizes of the components within them.

  1. The buttons at the bottom are throwing off your widths a bit since "Limpar lista de disciplinas" takes up more space than "Adicionar disciplina". If you first create Limpar, then you can set the preferred width of Adicionar based on Limpar's preferred width. i.e.

    JButton limparDisciplina = new JButton("Limpar lista de disciplinas");
    JButton adicionaDisciplina = new JButton("Adicionar disciplina");
    adicionaDisciplina.setPreferredSize(limparDisciplina.getPreferredSize());
    
  2. The labels on the left side are getting ipadx = 50, but on the right side the labels have ipadx = 0. This is why the text fields appear closer to the labels on the right side than they do on the left side. If the width is still a concern you can use the same trick of setting the preferred size for the labels or text fields like I suggested for the buttons.