Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:43:58+00:00 2026-05-27T21:43:58+00:00

I’m coding a prototype, but got problems with the GUI. I want the JPanel

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:43:58+00:00Added an answer on May 27, 2026 at 9:43 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.