Search code examples
javaswinglayout-managergridbaglayout

Using the GridBagLayout


I have never used layouts before and from what I understand GridBagLayout is probably not the easiest layout to start on, however I am trying to use it anyway. I am trying to use the GridBagLayout for my JPanel so that when I re-size it the components grow. I only need it to keep the natural height of the components but I am trying to use GridBagConstraints.HORIZONTAL to make the width of a component re-size automatically. I have the code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.io.*;

public class NumberConverterDesignTest
{
    JFrame numberConversionWindow = new JFrame("Number Conversion");

    JPanel  numberPanel = new JPanel();

    ////////COMPONENTS////////
    JLabel lblTitle;
    JLabel lblBase2;
    JLabel lblBase8;
    JLabel lblBase10;
    JLabel lblBase16;
    JTextField txtBase2;
    JTextField txtBase8;
    JTextField txtBase10;
    JTextField txtBase16;

    ///////////Font///////////
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String []fontFamilies = ge.getAvailableFontFamilyNames();
    Font FontT5 = new Font("SansSerif", Font.BOLD, 45);

    public void numberConvertGUI()
    {
        numberConversionWindow.setBounds(10, 10, 420, 300);

        numberConversionWindow.setMinimumSize(new Dimension(420, 300));

        numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        numberConversionWindow.setLayout(new GridLayout(1,1));

        createNumberConversionPanel();

        numberConversionWindow.getContentPane().add(numberPanel);

        numberConversionWindow.setVisible(true);
    }

    public void createNumberConversionPanel()
    {
        numberPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        lblTitle = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        lblTitle.setText("Number Converter");
        lblTitle.setHorizontalAlignment(JLabel.CENTER);
        lblTitle.setFont(FontT5);
        numberPanel.add(lblTitle, c);

        lblBase2 = new JLabel();
        c.weightx = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        lblBase2.setText("Binary: ");
        lblBase2.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase2);

        lblBase8 = new JLabel();
        //c.weightx = 0.5;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        lblBase8.setText("Octal: ");
        lblBase8.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase8);

        lblBase10 = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        lblBase10.setText("Decimal: ");
        lblBase10.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase10);

        lblBase16 = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        lblBase16.setText("Hexadecimal: ");
        lblBase16.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase16);

        txtBase2 = new JTextField();
        c.weightx = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        txtBase2.setText("");
        numberPanel.add(txtBase2);

        txtBase8 = new JTextField();
        //c.weightx = 0.5;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        txtBase8.setText("");
        numberPanel.add(txtBase8);

        txtBase10 = new JTextField();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        txtBase10.setText("");
        numberPanel.add(txtBase10);

        txtBase16 = new JTextField();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 4;
        txtBase16.setText("");
        numberPanel.add(txtBase16);
    }

    public static void main(String[] args)
    {
        NumberConverterDesignTest nC = new NumberConverterDesignTest();
        nC.numberConvertGUI();
    }
}

and when this is compiled I get

Incorrect Panel

My aim is to try and get

What I am trying to get

I believe the problem to be what I have set weightx to be, however I am not sure what to set it to, to get the result that I would like. Any help would be greatly appreciated


Solution

  • You need to pass the updated constraint each time you call the numberPanel.add method.