2

I have a JPanel that contains two JLabel. The panel uses a BorderLayout.

One JLabel is put into BorderLayout.CENTER position, the other in BorderLayout.PAGE_END

When I resize the panel so that it does not have enough vertical space to show both labels, the centered label is always overwritten (cut off) by the label in the PAGE_END position.

As the information displayed in the centered label is more important than the other, I would like the centered label to overlap (or cut off) the label below it.

It seems that BorderLayout (and GridBagLayout as well) always paints the components from "top to bottom" and those that are painted "later" will overwrite the ones painted before.

Is there some way I can convince BorderLayout (or any other LayoutManager) to assume that a certain component should always be "at the top"?

I tried using

panel.setComponentZOrder(label1, 1);
panel.setComponentZOrder(label2, 0);

but that didn't make a difference.

2 Answers 2

2

One approach would be to use a custom variation of GridLayout that respects preferred sizes. PreferredSizeGridLayout using the PreferredBoundable implementation is an example.

Addendum: Here's the test code I tried. Without change, the lower label "slides" beneath the upper, but you'll have to handle the horizontalAlignment property.

public class PreferredLayoutTest extends JPanel {

    public PreferredLayoutTest() {
        this.setLayout(new PreferredSizeGridLayout(0, 1));
        this.add(createLabel("One"));
        this.add(createLabel("Two"));
    }

    private JLabel createLabel(String s) {
        JLabel label = new JLabel(s);
        label.setOpaque(true);
        label.setBackground(Color.lightGray);
        label.setFont(label.getFont().deriveFont(36f));
        return label;
    }

    private void display() {
        JFrame f = new JFrame("PreferredLayoutTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PreferredLayoutTest().display();
            }
        });
    }
}
3
  • Thanks for the link. I was thinking about my own LayoutManager as well, but just didn't want to go down that road. Not sure if the PreferredSizeGridLayout will help as I actually want the second label to not honor the preferred size. But maybe I can put something together that honors the preferred size of the "main" label only.
    – user330315
    Feb 4, 2012 at 20:50
  • I've added my test code above. One appealing aspect of the design is the implicit strategy pattern afforded by the BoundableInterface. You might leave this open a while against a solution using a stock layout manager.
    – trashgod
    Feb 5, 2012 at 0:33
  • That's pretty cool. Thanks for the code. In the meantime I managed to do this using a BoxLayout (which simply pushes the lower label out of the container). But I'll mark your answer as answered anyway because I'm going to keep that code around ;)
    – user330315
    Feb 5, 2012 at 8:57
2

Call setMinimumSize(Dimension) on the top-level container when there is enough text in the center label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.