Remove huge gaps between check boxes on panel
Asked Answered
L

2

5

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

Lacagnia answered 28/12, 2014 at 23:16 Comment(3)
Have you tried to pack()?Culminate
Assuming you mean JFrame.pack(), it has no effectLacagnia
1) For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (Short, Self Contained, Correct Example). 2) I'd guess 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.Botanist
B
4

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);
    }
}
Botanist answered 29/12, 2014 at 1:31 Comment(0)
M
4

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 )

Maurilla answered 28/12, 2014 at 23:33 Comment(2)
I've removed JFrame.setSize(maxX, maxY) and the window opens 1x1 pixels, when I make it bigger the JCheckBoxs are still spacedLacagnia
@bobby: yes you will want to fix your layouts so that they work better. Perhaps use GridLayout for the JPanel that holds the check boxes, and use a BorderLayout for the JPanel that holds the main top level components and containers. Don't set sizes or preferred sizes, do call pack() after adding everything to the GUI and before setting it visible. Most important is that you read the layout manager tutorials.Vange
B
4

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);
    }
}
Botanist answered 29/12, 2014 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.