Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make MiGLayout behave like Wrap Layout?

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");  
    }
}
like image 825
Datoraki Avatar asked Apr 19 '11 11:04

Datoraki


People also ask

What is wrap layout?

<WrapLayout> is a layout container that lets you position items in rows or columns, based on the orientation property. When the space is filled, the container automatically wraps items onto a new row or column.

What is MiGLayout in Java?

MiGLayout is a grid-based layout underneath, as is FormLayout or GridBagLayout. However, it provides constraints that allow it to be used conceptually as a flowing or docking layout; it even provides absolute positioning of some components.


1 Answers

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.

like image 169
Datoraki Avatar answered Oct 31 '22 15:10

Datoraki