Question

I am trying to add a JTabbedPane to a panel, with buttons above it for controls. I am using MigLayout.

But, I cannot get the JTabbedPane to fill without the buttons also filling when they don't need to.

Take the following SSCCE:

public static void main(String[] args) {
    JPanel panel = new JPanel(new MigLayout(new LC().fill()));
    panel.add(new JButton("+"));
    panel.add(new JButton("-"), "wrap");
    panel.add(new JTabbedPane(), "span, grow");
    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(new Dimension(300, 500));
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

This produces a panel that looks like this:

enter image description here

But, if I remove fill() from the layout constraints (JPanel panel = new JPanel(new MigLayout(new LC()));), it looks like this:

enter image description here

How do I make the JTabbedPane to fill the content area while the JButtons do not fill?

Was it helpful?

Solution

If you don't want a constraint be applied to all cells, don't tell the Layout ;-) Instead, use column and row constraints: three columns and let only the last fill and grow and two rows with the last growing

JPanel panel = new JPanel(new MigLayout("", "[][][fill, grow]", "[][fill, grow]"));
panel.add(new JButton("+"));
panel.add(new JButton("-"), "wrap");
panel.add(new JTabbedPane(), "span, grow");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top