Search code examples
javaswinglayout-managermiglayout

Dock objects in the corner - MigLayout


How to dock three or more components in the corner eg. south-east using MigLayout ? What I' ve tried:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

public class App {

    public static void launchView(){
        JFrame frame = new JFrame("Foo");
        frame.setLayout(new MigLayout());
        JButton b1 = new JButton("Sample1");
        JButton b2 = new JButton("Sample2");
        JButton b3 = new JButton("Sample3");

        frame.add(b1, "dock south, east");
        frame.add(b2);
        frame.add(b3);
        frame.setSize(new Dimension(600, 200));
        frame.setVisible(true);

    }

    public static void main(String [] args){
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               launchView();
           }
       });
   }
}

enter image description here

In the picture you can see buttons showed in the left corner in the wrong order, but I would like to have them always in the south-west corner, one next to another (Button1 | Button2 | Button3).


Solution

  • Try using SceneBuilder, a plug in available for Eclipse and NetBeans.You can easily construct GUI's using that tool.

    A potential solution for yourself is to use this:

     frame = new JFrame();
     frame.setBounds(100, 100, 450, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.getContentPane().setLayout(new MigLayout("", "[grow,right]", "[grow,bottom]"));
    
     JButton btnNewButton = new JButton("New button");
     frame.getContentPane().add(btnNewButton, "flowx,cell 0 0");
    
     JButton btnNewButton_1 = new JButton("New button");
     frame.getContentPane().add(btnNewButton_1, "cell 0 0");
    
     JButton btnNewButton_2 = new JButton("New button");
     frame.getContentPane().add(btnNewButton_2, "cell 0 0");