Search code examples
javaswinglayout-managermiglayoutborder-layout

Please give a quick example how to simlate BorderLayout with MigLayout?


I wish WEST and EAST fractions of layout be of constant width. How to setup this with MigLayout?

UPDATE

The following code does not resize like border layout:

package testing.Test_MigLayout_01;

import javax.swing.JButton;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class App201210042244 
{

private class JFrame extends javax.swing.JFrame {

    {
        setLayout(new MigLayout());

        add(new JButton("1"), "dock east, width 120");
        add(new JButton("2"), "west");
        add(new JButton("3"), "center");
        add(new JButton("4"), "north");
        add(new JButton("5"), "south");

    }

}

public App201210042244() {
     SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame jFrame = new JFrame();
                jFrame.pack();
                jFrame.setVisible(true);

            }
        });
}


public static void main( String[] args )
{
   new App201210042244();
}
}

The size of window internals remains constant:

enter image description here


Solution

  • panel.add(new JButton("1"), "dock east, width 120");
    panel.add(new JButton("2"), "west");
    panel.add(new JButton("3"), "center");
    panel.add(new JButton("4"), "north");
    panel.add(new JButton("5"), "south");
    

    The dock keyword can be omitted.

    UPDATE:

    If you want all your components to expand when the window is resized:

    panel.setLayout(new MigLayout("fill"));
    panel.add(new JButton("1"), "dock east, width 120");
    panel.add(new JButton("2"), "west");
    panel.add(new JButton("3"), "center, grow");
    panel.add(new JButton("4"), "north");
    panel.add(new JButton("5"), "south");