Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove huge gaps between check boxes on panel

Its pretty basic UI, but I cannot setup the JCheckBox buttons so that they are placed immediately after one another (vertically) without any spacing. How would I reduce the spacing seen below?

JPanel debugDrawPanel = new JPanel(new GridLayout(0,1));         
JPanel eastPanel = new JPanel(new GridLayout(1,0));
JTabbedPane tab = new JTabbedPane();

click = new ClickPanel(this);

setSettings(new Settings());

for (Setting setting: getSettings().getAll()){

    JCheckBox checkBox = new JCheckBox(setting.name);
    checkBox.setName(setting.name);
    checkBox.addItemListener(new CheckBoxItemListener(this));
    debugDrawPanel.add(checkBox);
}

tab.addTab("Object Parameters", click);
tab.addTab("Debug Draw", debugDrawPanel);

screenshot

like image 266
bobbyrne01 Avatar asked Dec 28 '14 23:12

bobbyrne01


2 Answers

It appears that the minimum vertical size is being set by the content of another tab. One way to get around that is to put the GridLayout in the PAGE_START of a BorderLayout before putting the panel with border layout into the tabbed pane.

enter image description here

  • The panel with GridLayout has an orange BG.
  • The panel with BorderLayout has a yellow BG.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class TopAlignedComponents {

    private JComponent ui = null;

    TopAlignedComponents() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JTabbedPane tb = new JTabbedPane();
        ui.add(tb);
        Image spacer = new BufferedImage(300, 100, BufferedImage.TYPE_INT_RGB);
        tb.addTab("Spacer", new JLabel(new ImageIcon(spacer)));

        String[] labels = {"Shapes", "Joints", "AABBs"};
        JPanel checkPanel = new JPanel(new GridLayout(0, 1, 4, 4));
        checkPanel.setBackground(Color.ORANGE);
        for (String label : labels) {
            checkPanel.add(new JCheckBox(label));
        }

        JPanel checkConstrain = new JPanel(new BorderLayout());
        checkConstrain.setBackground(Color.YELLOW);
        checkConstrain.add(checkPanel, BorderLayout.PAGE_START);

        tb.addTab("Check", checkConstrain);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TopAlignedComponents o = new TopAlignedComponents();

                JFrame f = new JFrame("Top Aligned Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
like image 121
Andrew Thompson Avatar answered Sep 27 '22 21:09

Andrew Thompson


If i remember correctly, its because of your layout!

GridLayout divides your windowsize into equal parts, so i think you should either unset your windows size and use pack() or you could switch to a different layout.

( I assume your window's size is or minimum-size is set somewhere )

like image 24
JustOneJavaDev Avatar answered Sep 27 '22 21:09

JustOneJavaDev