Naseem
Naseem

Reputation: 961

How to keep 2 JPanels aligned vertically to each other?

Ok, I am pretty new to Java swings and is trying to learn the tricks of it but is struggling with the alignment of 2 JPanels in the frame. If you look at the below screenshot, my column name JPanel alignment and its below JPanel alignment is not aligning to each other properly.

Below is my code.

    JPanel columns = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 1.0;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.VERTICAL;


    text = "<html><b>Action</b></html>";        
    JLabel label_action = new JLabel(text);
    columns.add(label_action, gbc);
    columns.setLayout(new GridBagLayout());


    JPanel values= new JPanel(new GridBagLayout());
    values.setBorder(BorderFactory.createRaisedBevelBorder());  

    gbc.weighty = 1.0;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.VERTICAL;

  DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();

    model.addElement("click");
    model.addElement("Input Keys");

    final JComboBox<String> comboBox1 = new JComboBox<String>(model);
    AutoCompleteDecorator.decorate(comboBox1);
    comboBox1.setPreferredSize(new Dimension(200, 22));
    values.add(comboBox1, gbc);
    values.setLayout(new GridBagLayout());

enter image description here

Upvotes: 1

Views: 335

Answers (1)

Jan Bodnar
Jan Bodnar

Reputation: 11647

Don't even bother to work with GridBagLayout. It is a poorly designed layout manager that does not meet today's requirements. I recommend either MigLayout or GroupLayout.

Look how easy is to create your layout with MigLayout:

package com.zetcode;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;


public class MigLayoutAlignmentEx extends JFrame {

    public MigLayoutAlignmentEx() {

        initUI();
    }

    private void initUI() {

        setLayout(new MigLayout());

        JLabel lbl1 = new JLabel("Action");
        JLabel lbl2 = new JLabel("Choose Selector");
        JLabel lbl3 = new JLabel("Element Properties");
        JLabel lbl4 = new JLabel("Values");
        JLabel lbl5 = new JLabel("Element Name");
        JLabel lbl6 = new JLabel("Comments");

        JComboBox combo1 = new JComboBox();
        JComboBox combo2 = new JComboBox();

        JTextField field1 = new JTextField(15);
        JTextField field2 = new JTextField(15);
        JTextField field3 = new JTextField(15);
        JTextField field4 = new JTextField(15);

        add(lbl1);
        add(lbl2);
        add(lbl3);
        add(lbl4);
        add(lbl5);
        add(lbl6, "wrap");

        add(combo1, "w 150lp");
        add(combo2, "w 150lp");
        add(field1);
        add(field2);
        add(field3);
        add(field4, "wrap");

        pack();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutAlignmentEx ex = new MigLayoutAlignmentEx();
            ex.setVisible(true);
        });        
    }
}

Your UI is super easy to create; you just put the components into the cells. However, unlike in MigLayout, where the grid is precreated, in GridBagLayout you have to create each cell manually, which is difficult and error prone.

enter image description here

Upvotes: 2

Related Questions