Search code examples
javaswingborder-layout

Content in the lower-left corner with border layout


I want to add a status bar to my panel.

Actually, it looks like this:

enter image description here

But I want the text to be displayed in the left lower corner.

Here is my code:

private JPanel pnl1, pnl2;
    //pnl1 contains everything exept the status bar, pnl2 only status bar
private JLabel lab3; 
    //lab3 is the status bar
    (…)

public static void main(String[] args) {
    new Panel1();
}

public Panel1() {

    pnl1 = new JPanel();
    pnl2 = new JPanel();

    lab3 = new JLabel("Started.");          //Statusbar
    (…)
    pnl1.setLayout(new FormLayout(
            "40*($lcgap, 10dlu)",
            "30*($lgap, 10dlu)" 
            ));
    CellConstraints cc = new CellConstraints();

    pnl1.add(…)
    pnl2.add(lab3);

    this.add(pnl1);
this.add(pnl2, BorderLayout.PAGE_END);
    this.(…)

Thank you for your help!


Solution

  • You are adding the label into pnl2 without, it seems, changing the layout. The default layout for JPanel is FlowLayout, which uses a centered alignment.

    If you want the label to be aligned to the left, you can use a FlowLayout, making sure the alignment is set to LEADING:

        pnl2.setLayout(new FlowLayout(FlowLayout.LEADING));
        pnl2.add(lab3);
    

    Or you can use a different layout, for instance BorderLayout:

        pnl2.setLayout(new BorderLayout());
        pnl2.add(lab3);