Component's Sizes in GridBagLayout

65 views Asked by At

I'm having major issues aligning the components in GridBagLayout, yes I've read the tutorial by Oracle and tried changing the weightx.

This is how it looks like currently : enter image description here

Basically what I need to achieve is:

JTextFields "Nome" and "Filiação" to stretch all the way to the left, just like "Idade" and "Turma"

Bottom JButtons to be the same size, aligned in the middle.

I hope someone can point what I'm missing here.

Here's a sorta SSCCE:

 package test1;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test1 {
public static void main(String[] args) {

    JFrame jf = new JFrame("Test");
    JPanel jp = new JPanel(new GridBagLayout());
    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;
    jp.add(labelNome, gbc);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    jf.setSize(500, 550);
    jf.add(jp);

    jf.setVisible(true);

}

}
1

There are 1 answers

6
camickr On BEST ANSWER

JTextFields "Nome" and "Filiação" to stretch all the way to the left, just like "Idade" and "Turma"

Don't know what that means. They are aligned to the left just like the other text fields in that second column.

They may not appear as far left as the other text fields because the "Endereco" label length is longer, so the second column can only start where that ends.

If you want the text fields to have the same size then you need to specify the size of your text field and the GridBagLayout will respect the size. You do this by specifying the columns in the text field when you create the text field:

JTextField tfNome = new JTextField(10);