Remove the huge gaps between the checkboxes in the panel

Its a pretty simple user interface, but I can’t configure the JCheckBox buttons to JCheckBox right after each other (vertically) without any space. How to reduce the interval visible 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

+5
source share
2 answers

It seems that the minimum vertical size is set by the contents of another tab. One way around this is to put the GridLayout in PAGE_START from BorderLayout before placing the border layout panel in the tabbed panel.

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); } } 
+4
source

If I remember correctly, this is because of your layout!

GridLayout divides your windowsize into equal parts, so I think you need to either resize the window, or use the package (), or you can switch to another layout.

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

+4
source

Source: https://habr.com/ru/post/1210014/


All Articles