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 8695659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:05:30+00:00 2026-06-13T01:05:30+00:00

Whenever I Call my add corePanel or add electivePanel methods They simply replace the

  • 0

Whenever I Call my add corePanel or add electivePanel methods They simply replace the last button that was added instead off adding a new one along side in the 4 columns I have set in the Jpanel.

I am aware that I have named the variables Panel when they are really buttons but that is irrelevant.

I really need some help.

Thanks

EDIT:
NOW when I mouse over the buttons all the information (Jpanels) inside of them disappears?

package Assignment2.view;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import ams.model.Course;

public class CoursePanel extends JPanel 
{
private JButton corePanel = new JButton();
private JButton electivePanel = new JButton();
private JLabel ccLabel = new JLabel("Code: ");
private JLabel titleLabel = new JLabel("Title: ");
private JLabel cpLabel = new JLabel("Credit Points: ");
private JLabel prLabel = new JLabel("Prereqs: ");
private Border blackline;


public CoursePanel()
{   
    setLayout(new GridLayout(0,4));
}



public void addcorePanel(Course createcourse)
{
    // core panel properties
    blackline = BorderFactory.createLineBorder(Color.black,10);

    corePanel.setLayout(new GridLayout(4,1));
    corePanel.setBackground(Color.GRAY);
    corePanel.setBorder(blackline);
    corePanel.add(ccLabel);
    corePanel.add(titleLabel);
    corePanel.add(cpLabel);
    corePanel.add(prLabel);
    corePanel.setAlignmentX(LEFT_ALIGNMENT);
    corePanel.setAlignmentY(TOP_ALIGNMENT);

    //Needed to rebuild the prerequisites for display.
    String[] prereqs = null;
    String result = null;
    prereqs = createcourse.getPreReqs();

    if (prereqs != null)
    {
    StringBuilder builder = new StringBuilder();
    for(String s : prereqs) 
    {
        builder.append(s);
        builder.append("\n");
    }
    result = builder.toString();
    }
    ccLabel.setText("Code: " + createcourse.getCode());
    titleLabel.setText("Title: " + createcourse.getTitle());
    cpLabel.setText("Credit points: " + Integer.toString(createcourse.getCreditPoints()));
    prLabel.setText("Prereqs: " + result);

    add(corePanel);

}


public void addelectivePanel(Course createcourseE)
{


    //elective panel properties
    electivePanel.setLayout(new GridLayout(4,1));
    electivePanel.setBackground(Color.GRAY);
    electivePanel.add(ccLabel);
    electivePanel.add(titleLabel);
    electivePanel.add(cpLabel);
    electivePanel.add(prLabel);
    electivePanel.setAlignmentX(LEFT_ALIGNMENT);
    electivePanel.setAlignmentY(TOP_ALIGNMENT);

    //Needed to rebuild the prerequisites for display.
    String[] prereqs = null;
    String result = null;
    prereqs = createcourseE.getPreReqs();

    if (prereqs != null)
    {
    StringBuilder builder = new StringBuilder();
    for(String s : prereqs) 
    {
        builder.append(s);
        builder.append("\n");
    }
    result = builder.toString();
    }

    ccLabel.setText("Code: " + createcourseE.getCode());
    titleLabel.setText("Title: " + createcourseE.getTitle());
    cpLabel.setText("Credit points: " + Integer.toString(createcourseE.getCreditPoints()));
    prLabel.setText("Prereqs: " + result);


    add(electivePanel);


}

}

  • 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-06-13T01:05:31+00:00Added an answer on June 13, 2026 at 1:05 am

    This happens because your labels and buttons (corePanel, electivePanel) are class variables, so when you call for example addcorePanel, instead of creating a new JButton you’re manipulating the same corePanel button. You should move your variables inside the method body, like this:

    public void addcorePanel(Course createcourse)
    {
        JButton corePanel = new JButton();
        JLabel ccLabel = new JLabel("Code: ");
        JLabel titleLabel = new JLabel("Title: ");
        JLabel cpLabel = new JLabel("Credit Points: ");
        JLabel prLabel = new JLabel("Prereqs: ");
    
    // ....
    
    // ....
    }
    

    JLabels and JButtons cannot be shared so you must create new instances each time you call your methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following function. Whenever i call it that time it will increase
Whenever I call trigger(reloadGrid) on a JqGrid, it clears the existing table, displays a
Whenever I call my logger in a method, e.g. _logger.Debug(Connecting to database...); I get
I keep getting this error whenever I call gethostbyname() in my C code. ==7983==
I'm working on a Perl module and whenever I call the skip() method I
I'm using the facebook graph PHP sdk - whenever I call the $facebook->api method
Save one image path in the database... and whenever a call is made to
I have two Activities along with one Application class, and whenever I call my
whenever I try to call my ejb from a client, I get this error
Whenever we make a call to any action controller whether through a full post

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.