3

I just started using MigLayout for SWING in Java and I'm really liking it so far. The only thing, though, is that the dock parameters don't seem to work the way I thought they worked and I can't figure out what I'm doing wrong.

The problem is: I'm trying to add a JButton inside a JPanel and docking it to the right side using panel.add(button, "east");. While it actually makes it the rightmost component, it still only takes the same space as it would in a flowLayout. What I'd like it to do is stick to the right side of the panel.

Here's some compilable code that recreates the problem:

public class MigLayoutTest extends JFrame
{
  public MigLayoutTest()
  {
    setSize(500,500);

    JPanel panel = new JPanel(new MigLayout());
    panel.setBackground(Color.YELLOW);
    setContentPane(panel);
    panel.setSize(500,500);
    panel.add(new JButton("Dock east"), "east");
    panel.add(new JButton("No dock"));
  }

  public static void main(String[] args)
  {
    JFrame frame = new MigLayoutTest();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Here's what the output looks like: MigLayoutTest result

And here's where I'd want the "dock east" button: MigLayoutTest expected result

If I'm using the parameters wrong, I'd like it if someone could tell me how I'm supposed to make my button dock to the right side of the panel.

Thanks!

Jumbala
  • 4,764
  • 9
  • 45
  • 65

1 Answers1

4

You have to specify growth paarameters:

new MigLayout("", "[grow]", "[]")

Be careful though how you use it - it may not work the way you think it is. Here is a good read up on MigLayout features http://www.miglayout.com/QuickStart.pdf

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
  • Thanks, it works! I had read the quickstart PDF but I guess I didn't understand some of the details it contains. One question, though: why I have to use the square brackets? Usually it means optional parameters, but it doesn't seem like it's that way in MigLayout. Also, if I omit them, it works the same (or it seems like it does at least) – Jumbala Jul 10 '11 at 17:00
  • You don't have to if you have one column/row layout. Square brackets define multi-column/multi-row boundries. I suspect that you end up with multiple columns or rows in your layout anyway – Eugene Ryzhikov Jul 10 '11 at 17:28