How do you remove/reduce the spacing between checkboxes with MigLayout?
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:
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);
}
}
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With