Child expands past the bounds of its parent when height or width set to 100% using MigLayout in Java

StackOverflow https://stackoverflow.com/questions/22867992

Question

I'm trying to use MigLayout to set the height of certain elements to take up the full height of their parent's container using the expression "height 100%" however there is some kind of feedback loop in MigLayout that causes the height to jump to the maximum value when loaded. This issue only happens when the MiGPanel is embedded into a JScrollPane, if I remove the lines of code dealing with the JScrollPane and add the MiGPanel to my JFrame then the program runs as intended. I added the boolean statement in there to make this eaiser to see.

NOTE: I've seen this fix but I'd like to use a solution where I don't have a margin on the bottom of my program. How to prevent MigLayout from exceeding a container's bounds

Here is the code:

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;

public class StackOverFlowQuestion {
    public static void main(String[] args) {

        JPanel MiGPanel = new JPanel();
        MiGPanel.setBackground(Color.red);
        MiGPanel.setLayout(new MigLayout());  

        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        MiGPanel.add(greenPanel, "pos 0 0, width 33%, height 100%");

        JPanel yellowPanel = new JPanel();
        yellowPanel.setBackground(Color.yellow);
        MiGPanel.add(yellowPanel, "pos 33% 0, width 33%, height 100%");

        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        MiGPanel.add(bluePanel, "pos 66% 0, width 34%, height 100%");


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

        boolean scroll = true;
        if(scroll){
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.add(MiGPanel);
            scrollPane.setViewportView(MiGPanel);
            mainFrame.add(scrollPane);  
        }else{
            mainFrame.add(MiGPanel);
        }

        mainFrame.add(scrollPane);  
        mainFrame.pack();
        mainFrame.setSize(900, 600); 
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }
}
Was it helpful?

Solution

After many sleepless nights, I came up with a solution:

    greenPanel.setLayout(new BorderLayout());
    greenPanel.add(new JLabel("test"));
    MiGPanel.add(greenPanel, "pos 0 0 33% 100%");

    JPanel yellowPanel = new JPanel();
    yellowPanel.setBackground(Color.yellow);
    MiGPanel.add(yellowPanel, "pos 33% 0 66% 100%");

    JPanel bluePanel = new JPanel();
    bluePanel.setBackground(Color.blue);
    MiGPanel.add(bluePanel, "pos 66% 0 100% 100%");

Absolute positioned elements should not use with and height attributes, instead you should use the "pos x y x1 y1" method.

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