3

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chris Browne
  • 1,582
  • 3
  • 15
  • 33
  • I don't see any reason why you couldn't do it, it's just a MigLayout configuration purpose. Please post your code attempts, so that we could eventually help you in to see what's wrong. But personnally it would'nt do it that way, see my answer below. – Yanflea Feb 20 '12 at 11:50

2 Answers2

3

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);
}
Yanflea
  • 3,876
  • 1
  • 14
  • 14
  • Thanks, this is what I will do (more or less). In regards to your comment, I didn't think a code example was really necessary at this stage since it's more of a design/overall question than a specific code-related issue. Thanks again! – Chris Browne Feb 20 '12 at 11:57
  • you are mixing awt and swing (which is possible as of newer jdk versions but typically not what you want :-) – kleopatra Feb 21 '12 at 09:08
  • Feel free to propose your version. – Yanflea Feb 21 '12 at 09:16
  • huch .. simply don't mix if there isn't a very good reason (which I doubt, my guess is that it happened accidentally) BTW: there's no reason whatever to use a BoxLayout if you already have Mig in lane, it can handle button bars on a really high level (like keeping all at the same size, taking care of their sequence as recommended by OS guidelines ..) – kleopatra Feb 21 '12 at 09:30
  • 1
    "there's no reason whatever to use a BoxLayout..." : This is *your* assertion. I think in the contrary that it may be a good shape to separate the 'dynamic part' from the 'static' buttons, as it was explained that the Mig Form is created dynamically. That's why I said this is how I would implement it *personally*. I never said that the Mig layout was not able to manage the button part, I never said that THIS was the unique solution ! Read my first comment... – Yanflea Feb 21 '12 at 10:19
  • ...that said, I think your comment is constructive, I didn't want to offend you when I propose you to post your version ('huch'). Really, please explain what are the pitfalls of doing that way rather than just saying 'simply don't mix if there isn't a very good reason' and detail what would be a good reason for you. – Yanflea Feb 21 '12 at 10:19
0

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").

bigstones
  • 15,087
  • 7
  • 65
  • 82