How can I remove the vertical gap between two cells in MigLayout?

A very simple question: how to remove the vertical gap between two cells containing two JCheckBox

? I marked a gap in the picture with a red border.

gap

And here's 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);
    }
}

      

+3


source to share


3 answers


Minimum spaces are defined either in row / column constraints if they should only be applied between specific rows / columns:

new MigLayout("insets 0, debug", "", "[]0[]"));

      

(a little wondering that this didn't work for you? It's fine here :)

or in layoutContraints if they should be applied between all lines:



new MigLayout("insets 0, gapy 0, debug"));

      

BTW: The "encoding" layout should follow the same rules as all encoding, fi DRY :-) In particular, my rule is not to repeat component constraints if you can achieve the goal with layout / line constraints. In this example, you can get rid of all the constraints of the component except spanning:

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"));

      

+3


source


Ok, I 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?

+2


source


Another solution is to split the cell into two subitems, put checkboxes there and apply the component break 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's a screenshot:

Screenshot from example

0


source







All Articles