Evgenij Reznik
Evgenij Reznik

Reputation: 18594

BorderLayout.CENTER "disappears"

I'm coding a prototype, but got problems with the GUI. I want the JPanel pCustomer to be centered, but doing so it disappears completely. If I put it for example in the SOUTH, everything is fine.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JPanel implements ActionListener {

    private JPanel pTop = new JPanel();
    private JPanel pMenue = new JPanel();
    private JPanel pContent = new JPanel();
    private JPanel pCustomer = new JPanel();
    private JPanel pEnq = new JPanel();
    private JPanel pCustomerMenue = new JPanel();
    private JTextField tf1 = new JTextField();
    private JButton bCustomer = new JButton("Customer");
    private JButton bEnq = new JButton("Product");
    private JButton bCNew = new JButton("New Customer");
    private JLabel lCustomer = new JLabel("Customer");
    String[] customerString = {"--- SELECT -- ", "New Customer", "Edit Customer", "Delete Customer"};
    private JComboBox cb1 = new JComboBox(customerString);
    private JLabel lRes = new JLabel();
    String[] productString = {"--- SELECT -- ", "Sell Product", "Enquire Product", "Complain Product"};
    private JLabel lWelcome = new JLabel("Welcome to our System!");
    private JLabel lNo = new JLabel("Customer Number:   ");
    private JLabel lEnq = new JLabel("Enquiry");

    public Test() {
        this.setLayout(new BorderLayout());

        // pTop
        this.add(pTop, BorderLayout.NORTH);
        pTop.setLayout(new BorderLayout());
        pTop.add(lNo, BorderLayout.WEST);
        pTop.add(tf1, BorderLayout.CENTER);

        // pMenue
        this.add(pMenue, BorderLayout.WEST);
        pMenue.setLayout(new GridLayout(5, 1));
        pMenue.add(bCustomer);
        pMenue.add(bEnq);

        // pContent        
        this.add(pContent, BorderLayout.CENTER);
        pContent.add(lWelcome);
        pContent.setLayout(new BorderLayout());

        pContent.setBackground(Color.GREEN);

        // pCustomer
        pContent.add(pCustomer, BorderLayout.CENTER); // EAST, SOUTH, WEST works, but I want it to be centered.
        pCustomer.add(cb1);
        pCustomer.add(lRes);
        pCustomer.setVisible(false);
        pCustomer.setBackground(Color.blue);

        // pCustomerMenue
        pContent.add(pCustomerMenue, BorderLayout.NORTH);
        pCustomerMenue.add(bCNew);
        pCustomerMenue.setVisible(false);
        pCustomerMenue.setBackground(Color.red);

        // pEnq
        pContent.add(pEnq, BorderLayout.CENTER);
        pEnq.add(lEnq);
        pEnq.setVisible(false);

        // ---

        bCustomer.addActionListener(this);
        bEnq.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        lWelcome.setVisible(false);

        if (source == bCustomer) {
            init();
            pCustomer.setVisible(true);
            pCustomerMenue.setVisible(true);
            bCustomer.setEnabled(false);
        }
        if (source == bEnq) {
            init();
            pEnq.setVisible(true);
            bEnq.setEnabled(false);
        }
    }

    public void init() {
        pCustomer.setVisible(false);
        pCustomerMenue.setVisible(false);
        pEnq.setVisible(false);
        bCustomer.setEnabled(true);
        bEnq.setEnabled(true);
    }
}

If I remove these 3 lines:

pContent.add(pEnq, BorderLayout.CENTER);
pEnq.add(lEnq);
pEnq.setVisible(false);

I can even put it in the Centre and it works.

Upvotes: 1

Views: 1414

Answers (2)

Thomas
Thomas

Reputation: 88707

Read up about border layout. Basically you have 5 positions (NORTH, EAST, SOUTH, WEST, CENTER) and whenever you put a component into one of those positions any component already in that position is replaced.

Thus pContent.add(pEnq, BorderLayout.CENTER); will replace pCustomer with pEnq.

If you want both panels in the center you either need to put an intermediate panel into the center and then add the other panels to that one or use another layout manager, e.g. MiGLayout.

With MiGLayout your pContent layout might look like this:

pContent.setLayout(new MiGLayout());

pContent.add(pCustomerMenue, "pushx, wrap"); //fill the available width, new line after this component
pContent.add(pCustomer, "pushx, wrap"); //fill the available width, new line after this component
pContent.add(pEnq, "pushx"); //fill the available width

Upvotes: 2

bembii
bembii

Reputation: 259

You try to add two different Panels to the Center of the BorderLayout.

First you add

pContent.add(pCustomer, BorderLayout.CENTER); // EAST, SOUTH, WEST works, but I want it to be centered.

And a few Lines later you do:

pContent.add(pEnq, BorderLayout.CENTER);

So pEnq lays over pCustomer!

Upvotes: 2

Related Questions