Search code examples
swingmiglayout

miglayout: can't get right-alignment to work


Something's not right here. I'm trying to get the rightmost button (labeled "help" in the example below) to be right-aligned to the JFrame, and the huge buttons to have their width tied to the JFrame but be at least 180px each. I got the huge button constraint to work, but not the right alignment.

alt text

I thought the right alignment was accomplished by gapbefore push (as in this other SO question), but I can't figure it out.

Can anyone help me?

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

public class RightAlignQuestion {
    public static void main(String[] args) {
        JFrame frame = new JFrame("right align question");
        JPanel mainPanel = new JPanel();
        frame.setContentPane(mainPanel);

        mainPanel.setLayout(new MigLayout("insets 0", "[grow]", ""));

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new MigLayout("", "[][][][]", ""));
        for (int i = 0; i < 3; ++i)
            topPanel.add(new JButton("button"+i), "");
        topPanel.add(new JButton("help"), "gapbefore push, wrap");
        topPanel.add(new JButton("big button"), "span 3, grow");

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new MigLayout("",
              "[180:180:,grow][180:180:,grow]","100:"));
        bottomPanel.add(new JButton("tweedledee"), "grow");
        bottomPanel.add(new JButton("tweedledum"), "grow");

        mainPanel.add(topPanel, "grow, wrap");
        mainPanel.add(bottomPanel, "grow");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Solution

  • never mind, I got it: looks like there needs to be a gap constraint in the column spec, not at the component level:

        topPanel.setLayout(new MigLayout("", "[][][]push[]", ""));