Search code examples
javaswinglayout-managergrid-layoutjcheckbox

How to reduce the space between the 3 swing checkboxes?


I want to reduce the size between the components with in the Formatting group (left side on the image). How to do this?

enter image description here

JPanel formattingGroup = createGroupWithName("Formatting");
        formattingGroup.setMinimumSize(new Dimension(250, 20));
        formattingGroup.setLayout(new GridLayout(5, 0));
        add(formattingGroup);

        final JCheckBox showSurface = new JCheckBox("Show surface");
        showSurface.setSelected(true);
        formattingGroup.add(showSurface);

        final JCheckBox showTerrain = new JCheckBox("Show terrain");
        showTerrain.setSelected(true);
        formattingGroup.add(showTerrain);

        final JCheckBox showVehicleStatus = new JCheckBox("Show vehicle status");
        showVehicleStatus.setSelected(true);
        formattingGroup.add(showVehicleStatus);

        JPanel pnl = createGroupWithName("Depth Stretch");
        formattingGroup.add(pnl);
        JSlider slider = new JSlider(0, 10);
        pnl.add(slider);

Solution

  • When using a GridLayout all components are made the same size.

    You are adding a JPanel with a TitledBorder and a JSlider to the grid. Therefore the checkboxes will take the same vertical height as that panel.

    You need to use a different layout manager for the panel. Maybe a vertical BoxLayout.