Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align components center in the JPanel using GridBagLayout?

When I try to align my component it goes either left side or right side.

So I just want the solution to get rid of this problem, and also tell me how set the size of the panel as 400 x 350 pixel.

enter image description here

Here is my code....titleLabel and ResultLabel should be aligned in center

public TimeGui() {

    layout = new GridBagLayout();
    setSize(400, 350);  //**Its not working**
    setBackground(Color.LIGHT_GRAY);
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    setBorder(new TitledBorder(new EtchedBorder(), "Time Conversion") );

    setLayout(layout);
    layoutConstraints = new GridBagConstraints();       
    textField1 = new JTextField(10);
    textField2 = new JTextField(10);

    String[] names1 = {"Seconds", "Minutes", "Hours", "Days", "Weeks"};


    comboBox1 = new JComboBox<>(names1);
    comboBox2 = new JComboBox<>(names1);

    titleLabel = new JLabel("Time Conversion Unit", JLabel.CENTER);
    resultLabel = new JLabel("Result Label");
    equalLabel = new JLabel("=");

    convertButton = new JButton("Convert");


    layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
    Insets inset = new Insets(10, 10, 10, 10);
    layoutConstraints.anchor = GridBagConstraints.CENTER;

    addComponent(titleLabel, 0, 0, 2, 2, inset ); // I tried (0,1,2,2) 



    addComponent(comboBox1, 3, 0, 2, 3, inset);

    addComponent(comboBox2, 3, 2, 2, 3, inset);

    addComponent(textField1, 6, 0, 1, 2, inset);

    addComponent(equalLabel, 6, 1, 1, 2, inset);

    addComponent(textField2, 6, 2, 1, 2, inset);

    addComponent(resultLabel, 8, 1, 2, 1, inset);

    addComponent(convertButton, 10, 0, 2, 2, inset);

}

private void addComponent(Component component, int row,
        int column, int width, int height, Insets inset1) {
    layoutConstraints.gridx = column;
    layoutConstraints.gridy = row;
    layoutConstraints.gridwidth = width;
    layoutConstraints.gridheight = height;
    layoutConstraints.insets = inset1;
    layout.setConstraints(component, layoutConstraints);
    add(component);
}
}
like image 590
Zakir Hussain Avatar asked Jun 14 '13 05:06

Zakir Hussain


People also ask

How do you use insets in GridBagLayout?

Try something like this: GridBagLayout gbl=new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc=new GridBagConstraints(); gbc. insets = new Insets(10, 10, 10, 10); JLabel jl = new JLabel("This is a JLabel!", SwingConstants. CENTER); jl.

What is the difference between GridLayout and GridBagLayout in Java?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

What is anchor in GridBagLayout?

Some of the variables that can be set in GridBagConstraints are the following: anchor specifies the compass point where the component will be anchored. fill specifies whether and in what directions the component may expand. gridx identifies the column in which the component begins.

Which method do you use to add a component to a JPanel?

Use add() to place components in a panel. FlowLayout is the default layout manager for JPanel . Use setLayout() only if you want a different layout manager.


1 Answers

The problem is with your gridwidth and your fill properties...

enter image description hereenter image description here

Basically all I changed was...

addComponent(titleLabel, 0, 0, GridBagConstraints.REMAINDER, 2, inset); // I tried (0,1,2,2) 
addComponent(comboBox1, 3, 0, 1, 3, inset);
addComponent(comboBox2, 3, 2, 1, 3, inset);
addComponent(textField1, 6, 0, 1, 2, inset);
addComponent(equalLabel, 6, 1, 1, 2, inset);
addComponent(textField2, 6, 2, 1, 2, inset);
layoutConstraints.fill = GridBagConstraints.NONE;
addComponent(resultLabel, 8, 0, GridBagConstraints.REMAINDER, 1, inset);
addComponent(convertButton, 10, 0, GridBagConstraints.REMAINDER, 2, inset);

You could play around with a few of the others.

As for defining the actual size of the panel, the best you can do is to override the getPreferredSize method of the TimeGui...

@Override
public Dimension getPreferredSize() {
    return new Dimension(400, 350);
}

Which will "suggest" to the parent container what size you would like to be laid out to. Just remember, this is an optional value and layout managers are well within there rights to ignore it.

like image 116
MadProgrammer Avatar answered Oct 03 '22 12:10

MadProgrammer