JTextField size changes with JLabel text length

498 Views Asked by At

I want to create 3 JTextFields in a row with all being of equal size.But my first Text field gets so wide than the other two.
After adding each component at a time and seeing what causes the problem I realized that if I type a long text for the JLabel that's when this problem occurs.If I reduce the length of the text in the JLabel JTextFields get into the sizes I want.Here's my code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class Calculator1 extends JFrame {

JTextField value1=new JTextField("0");
JTextField value2=new JTextField("0");
JTextField value3=new JTextField("0");
JLabel label1=new JLabel("Label1 size of JTextfields chnge with Label length");
JTextField value4=new JTextField("0");
JTextField value5=new JTextField("0");
JTextField value6=new JTextField("0");
JLabel label2=new JLabel("Label2");
JLabel label3=new JLabel("Label3");
JTextField value7=new JTextField("0");
public Calculator1(){

    super("Calculator");

    setSize(500,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel=new JPanel();
    GridBagLayout bag=new GridBagLayout();
    getContentPane().add(panel);
    panel.setLayout(bag);
    GridBagConstraints c=new GridBagConstraints();

    c.insets=new Insets(5,5,5,5);
    c.fill=GridBagConstraints.HORIZONTAL;


    c.gridx=0;
    c.gridy=0;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridwidth=1;
    panel.add(label1,c);


    c.gridx=0;
    c.gridy=1;
    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    panel.add(value1,c);


    c.gridx=5;
    c.gridy=1;
    panel.add(value2,c);


    c.gridx=10;
    c.gridy=1;
    panel.add(value3,c);

    c.gridwidth=1;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=3;
    panel.add(label2,c);

    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=4;
    c.insets=new Insets(5,5,5,5);
    panel.add(value4,c);


    c.gridx=5;
    c.gridy=4;
    panel.add(value5,c);


    c.gridx=10;
    c.gridy=4;
    panel.add(value6,c);

    c.gridwidth=1;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=5;
    panel.add(label3,c);

    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=6;
    panel.add(value7,c);


    setVisible(true);
}
public static void main (String args[]){
    Calculator1 cal=new Calculator1();
}

}

How can I stop the JTextFields changing size when I change the JLabel label1's text length

2

There are 2 best solutions below

2
On BEST ANSWER

Set gridwidth = 3 for JLabel's and 1 for JTextField's like next :

public Calculator() {

    super("Calculator");

    setSize(500, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    GridBagLayout bag = new GridBagLayout();
    getContentPane().add(panel);
    panel.setLayout(bag);
    GridBagConstraints c = new GridBagConstraints();

    c.insets = new Insets(5, 5, 5, 5);
    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridwidth = 3;
    panel.add(label1, c);

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    panel.add(value1, c);

    c.gridx = 1;
    c.gridy = 1;
    panel.add(value2, c);

    c.gridx = 2;
    c.gridy = 1;
    panel.add(value3, c);

    c.gridwidth = 3;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 3;
    panel.add(label2, c);

    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 4;
    c.insets = new Insets(5, 5, 5, 5);
    panel.add(value4, c);

    c.gridx = 1;
    c.gridy = 4;
    panel.add(value5, c);

    c.gridx = 2;
    c.gridy = 4;
    panel.add(value6, c);

    c.gridwidth = 3;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 5;
    panel.add(label3, c);

    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 6;
    panel.add(value7, c);

    setVisible(true);
}

enter image description here

Also read tutorial for GridBagLayout

0
On

use grid layout. it will automatically make all are equal if you dont provide size . for ex :

setLayout(new GridLayout(rows,cols,vgap, hagap))