4

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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

3 Answers3

3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

You might look at available size variants, discussed in Resizing a Component.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Honest question: Isnt the resizing-option for components only possible in the [Nimbus look-and-feel](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/custom.html)? `"Nimbus is currently the only Sun look and feel that supports size variants"` – hamena314 Jan 25 '16 at 13:00
  • 1
    It's the only _Sun_ (now Oracle) L&F that supports size variants; `com.apple.laf.AquaLookAndFeel` does, too, for [example](http://stackoverflow.com/a/17392522/230513). – trashgod Jan 25 '16 at 14:52
0

Use gridbaglayout because that gives you the opportunity to give weights, to columns or rows and set spacing and padding values.

I made a Swing application that contains out of 12 Frames and they all are made with GridBagLayout.

I also tried other before that but they all had limits. That's where the GridBagLayout kicks in. It's a bit harder in begin to understand how it works, but once you get feeling with it, it really is best thing to get the components where you want.

If you want i'll give you a cool example of a frame created with GridBagLayout.

Kristof
  • 11
  • 2