pankti
pankti

Reputation: 195

Java Swing UI alignment issue

I have a requirement as per which the UI should get displayed as below image.

enter image description here

I have tried achieving the same by using the below piece of code.

package com.samples;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;


public class MyFrame extends JFrame {

    public MyFrame() {
    }

    public static void main(String[] args) {
        Runnable runner = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("My Sample Frame");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(1000, 800);
                frame.setVisible(true);

                JPanel jpOPanel = new JPanel();
                jpOPanel.setBorder(new CompoundBorder(new TitledBorder("Outer Panel"),
                                                      new EmptyBorder(0, 2, 2, 2)));
                GridBagLayout gbl = new GridBagLayout();
                GridBagConstraints gbc = new GridBagConstraints();
                jpOPanel.setLayout(gbl);

                JPanel jpSearch = new JPanel(new FlowLayout(FlowLayout.RIGHT));
                JButton jbSearch = new JButton("Search");
                jpSearch.add(jbSearch);
                gbc.weightx = 0.0D;
                gbc.weighty = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.fill = 1;
                gbc.gridwidth = 0;
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbl.setConstraints(jpSearch, gbc);
                jpOPanel.add(jpSearch);

                JLabel jLabel1 = new JLabel("Input 1:");
                gbc.weightx = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 5);
                gbc.gridwidth = -1;
                gbc.gridx = 0;
                gbc.gridy = 1;
                gbl.setConstraints(jLabel1, gbc);
                jpOPanel.add(jLabel1);

                JComboBox jComboBox1 = new JComboBox();
                gbc.weightx = 1.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridwidth = 0;
                gbc.gridx = 1;
                gbc.gridy = 1;
                gbl.setConstraints(jComboBox1, gbc);
                jComboBox1.setVisible(true);
                jpOPanel.add(jComboBox1);

                JLabel jLabel2 = new JLabel("Input 2:");
                gbc.weightx = 0.0D;
                gbc.weighty = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 5);
                gbc.fill = 2;
                gbc.gridwidth = -1;
                gbc.gridx = 0;
                gbc.gridy = 2;
                gbl.setConstraints(jLabel2, gbc);
                jpOPanel.add(jLabel2);

                JComboBox jComboBox2 = new JComboBox();
                gbc.weightx = 1.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridwidth = 0;
                gbc.gridx = 1;
                gbc.gridy = 2;
                gbl.setConstraints(jComboBox2, gbc);
                jComboBox2.setVisible(true);
                jpOPanel.add(jComboBox2);

                JLabel jLabel3 = new JLabel("Input 3:");
                gbc.weightx = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 5);
                gbc.gridwidth = -1;
                gbc.gridx = 0;
                gbc.gridy = 3;
                gbl.setConstraints(jLabel3, gbc);
                jpOPanel.add(jLabel3);

                JComboBox jComboBox3 = new JComboBox();
                gbc.weightx = 1.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridwidth = 0;
                gbc.gridx = 1;
                gbc.gridy = 3;
                gbl.setConstraints(jComboBox3, gbc);
                jpOPanel.setVisible(true);
                jpOPanel.add(jComboBox3);

                JPanel jpPanel1 = new JPanel();
                jpPanel1.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
                GridBagConstraints gbc3 = new GridBagConstraints();
                gbc.gridwidth = 1;
                gbc.gridx = 0;
                gbc.gridy = 9;
                gbl.setConstraints(jpPanel1, gbc);

                JLabel jLabel4 = new JLabel("Input 4:");
                gbc3.insets = new Insets(0, 0, 0, 0);
                gbc3.gridx = 0;
                gbc3.gridy = 0;
                gbc3.gridwidth = -1;
                jpPanel1.add(jLabel4, gbc3);

                JTextField jTextField1 = new JTextField();
                jTextField1.setEditable(false);
                jTextField1.setPreferredSize(new Dimension(100, 20));
                jTextField1.setMinimumSize(new Dimension(100, 20));
                jTextField1.setMaximumSize(new Dimension(100, 20));
                gbc3.insets = new Insets(0, 0, 0, 0);
                gbc3.gridx = 1;
                gbc3.gridy = 0;
                gbc3.gridwidth = 0;
                jpPanel1.add(jTextField1, gbc3);

                JLabel jLabel5 = new JLabel("Input 5:");
                gbc3.insets = new Insets(0, 0, 0, 0);
                gbc3.gridx = 2;
                gbc3.gridy = 0;
                gbc3.gridwidth = -1;
                jpPanel1.add(jLabel5, gbc3);

                JTextField jTextField2 = new JTextField();
                jTextField2.setEditable(false);
                jTextField2.setPreferredSize(new Dimension(100, 20));
                jTextField2.setMinimumSize(new Dimension(100, 20));
                jTextField2.setMaximumSize(new Dimension(100, 20));
                gbc3.insets = new Insets(0, 0, 0, 0);
                gbc3.gridx = 3;
                gbc3.gridy = 0;
                gbc3.gridwidth = 0;
                jpPanel1.add(jTextField2, gbc3);
                jpOPanel.add(jpPanel1);

                JLabel jLabel6 = new JLabel("Input 6:");
                gbc.weightx = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 5);
                gbc.gridwidth = -1;
                gbc.gridx = 0;
                gbc.gridy = 15;
                gbl.setConstraints(jLabel6, gbc);
                jpOPanel.add(jLabel6);

                JComboBox jComboBox4 = new JComboBox();
                gbc.weightx = 1.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridwidth = 0;
                gbc.gridx = 1;
                gbc.gridy = 15;
                gbl.setConstraints(jComboBox4, gbc);
                jpOPanel.add(jComboBox4);

                JLabel jLabel7 = new JLabel("Input 7:");
                gbc.weightx = 0.0D;
                gbc.insets = new Insets(0, 0, 5, 5);
                gbc.gridwidth = -1;
                gbc.gridx = 0;
                gbc.gridy = 16;
                gbl.setConstraints(jLabel7, gbc);
                jpOPanel.add(jLabel7);

                JTextField jTextField3 = new JTextField();
                jTextField3.setEditable(false);
                gbc.weightx = 1.0D;
                gbc.insets = new Insets(0, 0, 5, 0);
                gbc.gridwidth = 0;
                gbc.gridx = 1;
                gbc.gridy = 16;
                gbl.setConstraints(jTextField3, gbc);
                jpOPanel.add(jTextField3);

                JPanel jpPanel3 = new JPanel();
                jpPanel3.setBorder(new CompoundBorder(new TitledBorder(""), new EmptyBorder(0, 0, 0, 0)));
                GridBagConstraints gbc2 = new GridBagConstraints();
                gbc.gridwidth = 2;
                gbc.gridx = 0;
                gbc.gridy = 24;
                gbl.setConstraints(jpPanel3, gbc);

                JCheckBox jCheckBox1 = new JCheckBox("Check Me");
                jCheckBox1.setPreferredSize(new Dimension(140, 20));
                jCheckBox1.setMinimumSize(new Dimension(140, 20));
                jCheckBox1.setMaximumSize(new Dimension(140, 20));
                gbc2.ipady = 0;
                gbc2.weightx = 0.5D;
                gbc2.weighty = 0.0D;
                gbc2.gridwidth = 1;
                gbc2.anchor = GridBagConstraints.LINE_START;
                gbc2.insets = new Insets(0, 0, 0, 0);
                gbc2.gridx = 0;
                gbc2.gridy = 0;
                jpPanel3.add(jCheckBox1, gbc2);

                JLabel jLabel8 = new JLabel("Input 8: ");
                jLabel8.setPreferredSize(new Dimension(140, 20));
                jLabel8.setMinimumSize(new Dimension(140, 20));
                jLabel8.setMaximumSize(new Dimension(140, 20));
                gbc2.ipady = 0;
                gbc2.weightx = 0.5D;
                gbc2.weighty = 0.0D;
                gbc2.anchor = GridBagConstraints.LINE_START;
                gbc2.insets = new Insets(0, 0, 0, 0);
                gbc2.gridx = 0;
                gbc2.gridy = 6;
                jpPanel3.add(jLabel8, gbc2);

                JComboBox jComboBox5 = new JComboBox();
                jComboBox5.setEnabled(false);
                jComboBox5.setPreferredSize(new Dimension(230, 20));
                jComboBox5.setMinimumSize(new Dimension(230, 20));
                jComboBox5.setMaximumSize(new Dimension(230, 20));
                gbc2.gridx = 3;
                gbc2.gridy = 6;
                jpPanel3.add(jComboBox5, gbc2);
                jpOPanel.add(jpPanel3);

                frame.add(jpOPanel);

            }
        };
        EventQueue.invokeLater(runner);
    }
}

But the components are getting displayed as shown in the image below. Please help on how to rectify the same.

enter image description here

Upvotes: 0

Views: 314

Answers (2)

maxdev
maxdev

Reputation: 2566

Note: Please try to cut your code snippets down to make them easier readable for others.

Generally when using GridBagLayout you will have to use panels with a weight of 1 to fill out the spaces that you want empty. First, set gbc.fill = GridBagConstraints.BOTH;.

Then, add empty panels and use weights to fill out the space as you see in the image below. The panels above and below shall take all the place they get, so weighty = 1; and also span over two columns, so gridwidth = 2. The panels in the middle take the remaining height. The panel on the right side should then take the remaining width, so weightx = 1. Like this your panel will always take only the remaining space.

How to use the layouting

Upvotes: 5

nick zoum
nick zoum

Reputation: 7285

You could try adding a Box.createHorizontalGlue(), at the leftmost part, but you have to make sure that it has the greatest gridbagconstraints.weightx;

For example:

gbc.weightx = 1;//needs to be more than 0 so it works.
gbc.gridx = 4;//needs to be the largest gridx in the panel + 1
jpPanel3.add(Box.createHorizontalGlue(), gbc2);

Upvotes: 2

Related Questions