1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dan
  • 7,286
  • 6
  • 49
  • 114
  • If I was u, I'd use GridLayout(4,1); – JBALI May 10 '15 at 09:01
  • Not sure if this is useful in your case, but you can use the Panelmatic library to get this layout easily: (https://today.java.net/article/2012/01/11/panelmatic-101). I wrote that after dealing with too much raw GBL code :-) – Michael Bar-Sinai May 10 '15 at 09:11
  • you should give [miglayout](http://www.miglayout.com/) a try :) – KhaledMohamedP May 10 '15 at 09:13
  • @user3624028 A `GridLayout` would not provide the required GUI structure, since each column (which can contain one component) is of equal width. A combination of 2 `GridLayout` instances in the `LINE_START` and `CENTER` of a `BorderLayout` would work though. But for this I'd use `GridBagLayout` (or [`GroupLayout`](http://stackoverflow.com/a/21659516/418556)). – Andrew Thompson May 10 '15 at 09:28
  • @KhaledMohamedP While `MigLayout` is a great layout, this simple task can easily be achieved using J2SE layouts, without adding dependencies for any 3rd party layout APIs. – Andrew Thompson May 10 '15 at 09:30
  • @AndrewThompson I would appreciate your help again on this [question](http://stackoverflow.com/questions/30605685/making-a-jbutton-act-like-a-jmenu). Any help would be greatly appreciated – Dan Jun 02 '15 at 20:50

1 Answers1

2

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

Michael Bar-Sinai
  • 2,729
  • 20
  • 27