Question

I have a MigLayout that I'd like to add components to dynamically, while keeping two buttons at the bottom of the frame (because that's intuitive).

Firstly, I'd like to know if what I'm currently doing is the best way to go about it, and secondly how to get what I'm trying to do to actually work.

At the moment, I'm using the MigLayout's "grid" to position the dynamically-added components, and then using the MigLayout's "border" to position the fixed components but I can't get both buttons to sit on the south border next to one another.

According to the Quickstart PDF, this should be possible (and I quote, "you aren't confined to use only one component per side") but it doesn't go on to say how you achieve this.

Was it helpful?

Solution

Personnally I'd rather split my JFrame in 2 JPanels with a BorderLayout. Place the MigLayout form within a JPanel in the CENTER area, and the the buttons within a Box in the SOUTH area.

EDIT

With an example it even better ;-)

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());

    // == MigLayout Form ==
    JPanel panelCenter = new JPanel();
    panelCenter.setLayout(
                new MigLayout(
                        new LC().wrapAfter(4),
                        new AC().size(":80:", 0).size("115:115:115", 1, 2, 3).align("right", 0, 2),
                        new AC().size("19:19:19")
                ));
    panelCenter.setOpaque(false);

    panelCenter.add(new JLabel("Label1"));
    panelCenter.add(new JTextField(), new CC().growX());
    panelCenter.add(new JLabel("Label2"));
    panelCenter.add(new JTextField(), new CC().growX());
    panelCenter.add(new JLabel("Label3"));
    panelCenter.add(new JTextField(), new CC().growX());
    panelCenter.add(new JLabel("Label4"));
    panelCenter.add(new JTextField(), new CC().growX());
    panelCenter.add(new JLabel("Label5"));
    panelCenter.add(new JTextField(), new CC().growX());
    panelCenter.add(new JLabel("Label6"));
    panelCenter.add(new JTextField(), new CC().growX());

    frame.add(panelCenter, BorderLayout.CENTER);

    // == Buttons ==
    Box southPanel = Box.createHorizontalBox();
    southPanel.add(Box.createHorizontalGlue());
    southPanel.add(new JButton("Ok"));
    southPanel.add(new JButton("Cancel"));
    southPanel.add(Box.createHorizontalGlue());
    frame.add(southPanel, BorderLayout.SOUTH);

    frame.setVisible(true);
    frame.setSize(500, 150);
}

OTHER TIPS

I did it like this:

create = new JButton("Create");
create.addActionListener(this);
mainPanel.add(create, "tag ok, span, split 2, sg btn");

cancel = new JButton("Cancel");
cancel.addActionListener(this);
mainPanel.add(cancel, "tag cancel, sg btn");

This is actually just the last row of my grid, but the key seems to be using span and split (sg just groups the sizes of the buttons, and tag positions them - lovely feature). I've found the example here (search "button bar").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top