Very simple question: How can I remove the vertical gap between the two cells containing the two JCheckBox
? I have marked the gap in the picture with a red border.
![](https://www.manongdao.com/static/images/pcload.jpg)
And here is the code:
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class Main {
public static void main(String[] args) {
JPanel somePanel = new JPanel();
somePanel.setLayout(new MigLayout("insets 0, debug", "", ""));
somePanel.add(new JCheckBox("first option"), "h 20!");
somePanel.add(new JButton("click me"), "spany 2, h 40!, w 60%, wrap");
somePanel.add(new JCheckBox("option two"), "h 20!");
JFrame frame = new JFrame();
frame.setContentPane(somePanel);
frame.pack();
frame.setVisible(true);
}
}
Minimum gaps are defined in either in the row/column constraints if they should be applied between particular rows/columns only:
new MigLayout("insets 0, debug", "", "[]0[]"));
(wondering a bit that this didn't work for you? It's fine here :)
or in the layoutContraints if they should be applied between all rows:
new MigLayout("insets 0, gapy 0, debug"));
BTW: layout "coding" should follow the same rules as all coding, f.i. DRY :-) In particular, my rule is to not repeat component constraints if you can achieve the goal with layout/row constraints. In the example you can get rid of all component constraints except the spanning by:
somePanel.setLayout(new MigLayout("insets 0, debug, wrap 2",
"[][60%, fill]", "[20!, fill]0"));
somePanel.add(new JCheckBox("first option"));
somePanel.add(new JButton("click me"), "spany 2");
somePanel.add(new JCheckBox("option two"));
Ok, I just found a good solution using docking:
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class Main {
public static void main(String[] args) {
JPanel somePanel = new JPanel();
somePanel.setLayout(new MigLayout("insets 0, debug", "", ""));
somePanel.add(new JButton("click me"), "east");
somePanel.add(new JCheckBox("first option"), "north");
somePanel.add(new JCheckBox("option two"), "south");
JFrame frame = new JFrame();
frame.setContentPane(somePanel);
frame.pack();
frame.setVisible(true);
}
}
But how would I do it, if docking is not an option?
Another solution is to split the cell into two subcells, place the check boxes there, and apply a component gap constraint.
package com.zetcode;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
/*
Demonstrating component gaps in MigLayout manager.
Author: Jan Bodnar
Website: zetcode.com
*/
public class MigLayoutGapsEx extends JFrame {
public MigLayoutGapsEx() {
initUI();
}
private void initUI() {
JCheckBox cb1 = new JCheckBox("First option");
JCheckBox cb2 = new JCheckBox("Second option");
JButton btn = new JButton("Click me");
createLayout(cb1, cb2, btn);
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout());
add(arg[0], "split 2, flowy");
add(arg[1], "gapy 0");
add(arg[2]);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MigLayoutGapsEx ex = new MigLayoutGapsEx();
ex.setVisible(true);
});
}
}
Here is the screenshot:
![](https://www.manongdao.com/static/images/pcload.jpg)