tags:

views:

318

answers:

2

The following is close to what I want, and does what I expect:

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class MigBug extends JFrame {

    public static void main(String args[]) {
        MigBug migbug = new MigBug();
        migbug.pack();
        migbug.setVisible(true);
    }

    public MigBug() {
        JPanel content = new JPanel();
        content.setLayout(new MigLayout("fill, debug"));

        content.add(new JLabel("Label 1"));
        content.add(new JComboBox());

        content.add(new JLabel("Label 2"));
        content.add(new JTextField(25), "growx, wrap");

        content.add(new JLabel("BIG"), "span, w :400:, h :200:, growy");

        setContentPane(content);
    }
}

However, if I make the following change:

content.add(new JLabel("BIG"), "span, w :400:, h :200:, grow");

ie. Change the spanned component to grow in x as well as y, the Label 1 cell grows in x, even though it shouldn't.

Does anyone know a way I can get round this?

+1  A: 

Found a workaround, though not entirely satisfactory. According to this forum post and this forum post, MigLayout switches from calculating component sizes, to calculating column sizes where a span is involved. Replacing "fill" with "filly" in the layout contraints, then adding column constraints with "grow" for each column that should be allowed to grow seems to fix it.

Working code:

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class MigBug extends JFrame {

    public static void main(String args[]) {
        MigBug migbug = new MigBug();
        migbug.pack();
        migbug.setVisible(true);
    }

    public MigBug() {
        JPanel content = new JPanel();
        content.setLayout(new MigLayout("filly, debug", "[][grow][][grow]"));

        content.add(new JLabel("Label 1"));
        content.add(new JComboBox());

        content.add(new JLabel("Label 2"));
        content.add(new JTextField(25), "growx, wrap");

        content.add(new JLabel("BIG"), "span, w :400:, h :200:, grow");

        setContentPane(content);
    }
}
Draemon
A: 

You could also try my MatrixLayout layout manager as an alternative. The concept is the similar as MiG Layout - table based. It's not as powerful, but it seems (to me, anyway) much simpler to use (with great power comes great complexity). But, to be honest, that might just be because I haven't tried hard to understand MiG Layout.

Software Monkey