ryvantage
ryvantage

Reputation: 13486

Keep MigLayout components from spacing out

I have this SSCCE:

public class FiltersPanel extends JPanel {

    public FiltersPanel() {
        init();
    }

    class Filter {
        String name;
        FilterType type;

        public Filter(String name, FilterType type) {
            this.name = name;
            this.type = type;
        }
    }

    enum FilterType {
        Money("$"),
        Percent("%"),
        Integer("#");
        String sign;

        private FilterType(String sign) {
            this.sign = sign;
        }
    }

    LinkedHashSet<Filter> filters;

    void addFilter(Filter filter) {

        JCheckBox checkBox_min = new JCheckBox(filter.name + " Min (" + filter.type.sign + ")");
        JTextField textField_min = new JTextField();
        JCheckBox checkBox_max = new JCheckBox(filter.name + " Max (" + filter.type.sign + ")");
        JTextField textField_max = new JTextField();
        add(checkBox_min, "growx");
        add(textField_min, "growx");
        add(checkBox_max, "growx");
        add(textField_max, "growx, span");
    }

    void init() {
        setLayout(new MigLayout(new LC().fill()));

        JCheckBox date_min_checkBox = new JCheckBox("Date Min:");
        JDateChooser date_min_dateChooser = new JDateChooser();
        JCheckBox date_max_checkBox = new JCheckBox("Date Max:");
        JDateChooser date_max_dateChooser = new JDateChooser();

        int row = 0;
        add(date_min_checkBox, "growx");
        add(date_min_dateChooser, "growx");
        add(date_max_checkBox, "growx");
        add(date_max_dateChooser, "growx, span");

        addFilter(new Filter("Sales Amt", FilterType.Money));
        addFilter(new Filter("Sales Qty", FilterType.Integer));
        addFilter(new Filter("SO Count", FilterType.Integer));

        addFilter(new Filter("Quotes Amt", FilterType.Money));
        addFilter(new Filter("Quotes Qty", FilterType.Integer));
        addFilter(new Filter("CQ Count", FilterType.Integer));

        addFilter(new Filter("Profit Margin", FilterType.Percent));
        addFilter(new Filter("Profit Total", FilterType.Money));
        addFilter(new Filter("Price", FilterType.Money));
        addFilter(new Filter("STQ Ratio", FilterType.Percent));

        addFilter(new Filter("QTY", FilterType.Integer));
        addFilter(new Filter("Inv Cost", FilterType.Money));


        JCheckBox pn_checkBox = new JCheckBox("Part #");
        pn_checkBox.setBorder(new LineBorder(Color.yellow));
        pn_checkBox.setBorderPainted(true);
        JRadioButton startsWith_button = new JRadioButton("Starts With");
        startsWith_button.setBorder(new LineBorder(Color.yellow));
        startsWith_button.setBorderPainted(true);
        JRadioButton contains_button = new JRadioButton("Contains");
        contains_button.setBorder(new LineBorder(Color.yellow));
        contains_button.setBorderPainted(true);
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(startsWith_button);
        buttonGroup.add(contains_button);
        JTextField pn_textField = new JTextField();
        pn_textField.setBorder(new LineBorder(Color.yellow));
        add(pn_checkBox, "newline, growx 0");
        add(startsWith_button, "growx 0");
        add(contains_button, "growx 0");
        add(pn_textField, "spanx, growx 100");


        JCheckBox conditions_checkBox = new JCheckBox("Conditions:");
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame();
        frame.add(new FiltersPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Which produces this:

enter image description here

I am trying to get the components in yellow to not have the gap between them and to have the last JTextField span the rest of the row.

This is the code I'm focusing on:

add(pn_checkBox, "newline, growx 0");
add(startsWith_button, "growx 0");
add(contains_button, "growx 0");
add(pn_textField, "spanx, growx 100");

The goal here was to say growx 0 for all the components except the JTextField (and that is growx 100).

Also tried this:

add(pn_checkBox, "newline, align left, push, growx 0");
add(startsWith_button, "align left, push, growx 0");
add(contains_button, "align left, push, growx 0");
add(pn_textField, "spanx, growx 100");

Nothin doin'

How would you do this?

Upvotes: 0

Views: 50

Answers (1)

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

As per the MigLayout Quick Start Guide you have to split and span your cells:

add(pn_checkBox, "newline, split 3");
add(startsWith_button);
add(contains_button);
add(pn_textField, "span 3, grow");

Explanation: Splitting the first cell means that the next two components are added into the same cell. This means there are still three cells left in the row, so you have to span the text field over these cells.

Upvotes: 1

Related Questions