MIGLayout - adding JSeparator

432 Views Asked by At

I am struggling with MIGLayout - I generally know its basics, but today encountered a problem.

To simplify the situation:

I want to have three rows, such that in the first and third row there is a non-fixed amount of objects (e.x. JTextField components). Width of first text field from first row should be the same as width of first text field from the third row and so on.

The last text field in row, which contains the most of them, should grow for the rest of frame size.

These two rows should be distinguished by JSeparator, which also should be of length of frame.

I have already seen How to draw a separator across a panel using MigLayout, but since I need the fields to be aligned exactly one below another, it doesn't work for me.

This is my code:

public class Main extends JFrame {
    public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            new Main();
    }

    public Main() {
            setLayout(new MigLayout(new LC()));
            setPreferredSize(new Dimension(800, 300));
            CC cc = new CC().growX();
            CC wcc = new CC().growX().wrap();
            add(new JTextField("Area 1"), cc);
            add(new JTextField("Area 2"), cc);
            add(new JTextField("Area 3"), wcc);
            add(new JSeparator(), new CC().spanX().pushX().growX().wrap());
            add(new JTextField("Longer Area 1"), cc);
            add(new JTextField("Longer Area 2"), cc);

            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
    }
}

This is how it looks like:

enter image description here

and this is how I want it to looked like:

enter image description here

What is the best solution to this problem? Amount of text fields will be changing dynamically. Thanks in advance.

1

There are 1 best solutions below

0
On

Use a layout constraint called fill and make everything growx. Then add spanx to the last component on each row.

The reason your example looked strange was because you were using pushx over fill.

The following example produces this output.

enter image description here

Runnable Example

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class TextFieldsForEveryone extends JFrame {
    private JPanel panel;

    private TextFieldsForEveryone() {
        setPreferredSize(new Dimension(800, 300));
        setUp();
        add(panel);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private void setUp() {
        panel = new JPanel(new MigLayout("fillx"));
        panel.add(new JTextField("Area 1"), "growx");
        panel.add(new JTextField("Area 2"), "growx");
        panel.add(new JTextField("Area 3"), "growx, spanx, wrap");
        panel.add(new JSeparator(), "growx, spanx, wrap");
        panel.add(new JTextField("Longer Area 1"), "growx");
        panel.add(new JTextField("Longer Area 2"), "growx, spanx");
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new TextFieldsForEveryone());
    }
}