6

I'd like to replicate the example shown here:

Wrap Layout

Using MiGLayout. I have tried some combinations, but I'm having a hard time making the buttons wrap automatically to new rows as the container shrinks.

Could someone please provide a working example doing this?

Here is a shell for the program:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class MiGTest extends JFrame{
    private JPanel jPanel;
    private JButton jButton;

    public static void main(String[] args) {
        new MiGTest().setVisible(true);
    }

    public MiGTest(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new MigLayout("debug"));

        initComponents();
        addComponents();
        pack();
    }


    private void addComponents() {      
        add(jPanel);{
            for (int i = 0; i < 10; i++) {
                jPanel.add(new JButton("" + i));
            }
        }
    }

    private void initComponents() {
        jPanel = new JPanel(new MigLayout("debug"));
        jButton = new JButton("Test");  
    }
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Datoraki
  • 1,223
  • 13
  • 26
  • It sounds as if you're trying to push a square peg into a round hole. Why use MiGLayout when you want a different layout effect? – Hovercraft Full Of Eels Apr 19 '11 at 12:04
  • 2
    @Hovercraft Quoted from miglayout.com: "You will never have to switch to another layout manager ever again!". If it turns out MigLayout can't do this, I will use Wrap Layout instead. But I am mostly curious to see if this is possible. – Datoraki Apr 19 '11 at 12:22
  • I think MiGLayout supports this and calls it in-cell flow: have a single cell that contains multiple components and they will flow like this. – Joachim Sauer Apr 19 '11 at 12:46
  • @Sauer: I tried this now, but it seems as if the components aren't wrapped at all inside a cell. Could you provide an example? – Datoraki Apr 19 '11 at 15:06

2 Answers2

9

According to the creators of MiGLayout and the answers to the following questions:

http://migcalendar.com/forums/viewtopic.php?f=8&t=3421

http://migcalendar.com/forums/viewtopic.php?f=8&t=2270&hilit=wrap+container

http://migcalendar.com/forums/viewtopic.php?f=8&t=2015&hilit=wrap+container

http://migcalendar.com/forums/viewtopic.php?f=8&t=1137&hilit=wrap+container

, MiGLayout quite simply doesn't support this. Neither does it support wrapping within a single cell.

Datoraki
  • 1,223
  • 13
  • 26
0

I've been declaring the .width("xx%") and then calling wrap when the row's widths add up to 100%. This works if you can declare each component to be a certain percentage of a row (they don't all have to be the same percentage).

sdasdadas
  • 23,917
  • 20
  • 63
  • 148