5

How do you remove/reduce the spacing between checkboxes with MigLayout?

alt text

import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("checkbox spacing");
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout());
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            JCheckBox cb = new JCheckBox(verb+" no evil");
            panel.add(cb, "wrap");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

NOTE: I'm wondering maybe if the MigLayout part of this question is a red herring. If I change my program to the following, I get these dialogs:

alt text alt text

You will note the difference in spacing between labels and checkboxes. How can I get rid of the excess spacing for checkboxes?

import java.awt.Component;
import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    enum WhichGUI { LABEL {
        @Override
        public Component createComponent(String text) {
            return new JLabel(text);
        }
    }, CHECKBOX {
        @Override
        public Component createComponent(String text) {
            return new JCheckBox(text);
        }
    };
        abstract public Component createComponent(String text); 
    }
    public static void main(String[] args) {
        doit(WhichGUI.LABEL);
        doit(WhichGUI.CHECKBOX);
    }
    private static void doit(WhichGUI which) {
        JFrame frame = new JFrame("spacing: "+which);
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout(
                  "",  //layout
                  "[]", //column
                  "0"  //row
            ));
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            Component c = which.createComponent(
                      verb+" no evil (Jabberwocky! Çgpq)");
            panel.add(c, "wrap 0");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Jason S
  • 184,598
  • 164
  • 608
  • 970

2 Answers2

5

The difference is in the preferred size of each component.

String text = "Hear no evil";
JLabel label = new JLabel(text);
System.out.println( label.getPreferredSize() );
System.out.println( label.getInsets() );

JCheckBox checkBox = new JCheckBox(text);
System.out.println( checkBox.getPreferredSize() );
System.out.println( checkBox.getInsets() );

A check box has non-zero insets. This would be from the border. So you should be able to either:

  1. set the border of the check box to null
  2. add an EmptyBorder to the label
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    thanks! I finally had time to try it, and setting the checkbox to null reduced the empty space. It looks like you need a little space for the focus rectangle to render properly, so I did `cb.setBorder(new EmptyBorder(new Insets(1,1,1,1)));` and that seems to be a nice compromise. – Jason S Feb 11 '11 at 18:36
1

There are a number of ways to accomplish this and they are well specified in this document.

You can specify the spacing either between the grid or the components. You probably want to do something like this:

     new MigLayout(
              "",  //layout
              "[]", //column
              "5"  //row
        );

And that should set the gaps. You will have to play with the numbers until you get what you want.

A second option (and I think you have more control this way is to do this:

  panel.add(cb, "wrap 0");

Where the 0 specifies the gap between rows.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • It still doesn't work. Maybe it's not a miglayout thing, maybe it's a checkbox thing instead: if I use your suggestion to increase the row spacing to 100, I get more spacing. But if I change the row spacing to 0, I get more or less what I started with, and there's extra spacing I don't want. – Jason S Jan 12 '11 at 15:56
  • If I use "wrap -3" then it looks about right, but there's something fishy about using a number less than 0. – Jason S Jan 12 '11 at 15:59
  • @Jason S You may want to take a look at the component gap then. You have several options such as gaptop, gapleft etc to specify how far the component is away from the edge. – Vincent Ramdhanie Jan 12 '11 at 16:03