How to reduce the space between JCheckboxes in GridLayout

I have three Java JCheckboxes in a column sorted by setting the layout of the JPanel container to GridLayout(3, 1, 1, 1) . When I run the program, there is too much vertical space between the JCheckBoxes; It looks larger than 1 pixel. Since I already set the vertical space between the JCheckboxes in the layout as 1 pixel, how else can I reduce the vertical space between these JCheckboxes?

Thanks.

+4
source share
3 answers

I have studied using GridLayout , BorderLayout and GridBagLayout , and I believe that any additional vertical space present in your application is related to the size of the JCheckBox component, not related to the layout manager. All of the examples below do not take place between components in the layout manager.

Gridlayout

 //Changing to 3,1,1,0 makes slightly smaller (1 pixel) gap vertically GridLayout layout = new GridLayout( 3, 1, 1, 0 ); JPanel main = new JPanel( layout ); main.add( new JCheckBox( "box 1" ) ); main.add( new JCheckBox( "box 2" ) ); main.add( new JCheckBox( "box 3" ) ); 

GridBagLayout

 GridBagConstraints gbc = new GridBagConstraints(); JPanel main = new JPanel( new GridBagLayout() ); gbc.gridx=0; gbc.gridy=0; gbc.ipady=0; main.add( new JCheckBox( "box 1" ), gbc ); gbc.gridy=1; main.add( new JCheckBox( "box 2" ), gbc ); gbc.gridy=2; main.add( new JCheckBox( "box 3" ), gbc ); 

Borderlayout

 JPanel main = new JPanel( new BorderLayout() ); main.add( new JCheckBox( "box 1" ), BorderLayout.NORTH ); main.add( new JCheckBox( "box 2" ), BorderLayout.CENTER ); main.add( new JCheckBox( "box 3" ), BorderLayout.SOUTH ); 
+3
source

Does this help when setting the frame?

 JCheckBox checkBox = new JCheckBox(); checkBox.setBorder(BorderFactory.createEmptyBorder()); 

This may also be due to the introduction of the Look and Feel UI delegate. Usually you have little control over this.

+3
source

Thanks Steve and Alex. Both of your answers were correct. By setting the border to an empty frame, I was able to move the flags closer.

0
source

All Articles