I got a panel with a dynamic width. Components added to the panel should be arranged left-to-right up to 4 components (cells) per row, where each component/cell fill 25% of the panels width. If there is only one component, it should fill 25% of the parents width - the remaining 75% (3 cells) of the space should be empty.
I got it to work using a hack, but I'm not happy about that implementation - using cells and creating an "empty" panel for each cell that is not used. See the code snippet below:
MigLayout migLayout = new MigLayout("fillx, insets 0, wrap 4", "", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel("1");
JLabel label2 = new JLabel("2");
JPanel filler1 = new JPanel();
JPanel filler2 = new JPanel();
panel.add(label1, "cell 0 0);
panel.add(label2, "cell 1 0);
panel.add(filler1, "cell 2 0);
panel.add(filler2 , "cell 3 0);
This gives something that can be illustrated like below, where the outer bracket is the outer panel, and the two inner brackets are the labels:
[ [ 1 ] [ 2 ] ]
Instead of using fillers I was hoping that it could be set with constraints, something like this:
MigLayout migLayout = new MigLayout("always 4 cells, fillx", "[fill][fill][fill][fill]", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
panel.add(label1);
panel.add(label2);
I have tried various layout constraints and add component parameters, but they are typically formatted like this:
[ [ 1 ] [ 2 ] ]