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

  • SEARCH
  • Home
  • 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 7006849
In Process

The Archive Base Latest Questions

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

I got this as home exercise but I have no clue how to solve

  • 0

I got this as home exercise but I have no clue how to solve it… (Just the access part is kind of difficult to figure out)

So, I have two classes: “Top” and “Main”. The Top class looks like this: (Short explanation: The Top class’ variable “pane” will be used in the class Main as the top panel, Main extends JFrame and has a BorderLayout)

public class Top extends JPanel implements ActionListener {

//public JPanel pane;
public JButton red, green, blue, white, black;

public Top() {
    //pane = new JPanel(); //Useless, as Top is already a JPanel
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setBackground(new Color(0xc9c9c9)); //A gray background

    red = new JButton("Red");
    red.setBackground(Color.red);
    red.addActionListener(this);
    this.add(red);

    green = new JButton("Green");
    green.setBackground(Color.green);
    green.addActionListener(this);
    this.add(green);

    blue = new JButton("Blue");
    blue.setBackground(Color.blue);
    blue.addActionListener(this);
    this.add(green);

    white = new JButton("White");
    white.setBackground(Color.white);
    white.addActionListener(this);
    this.add(white);

    black = new JButton("Black");
    black.setForeground(Color.white);
    black.setBackground(Color.black);
    black.addActionListener(this);
    this.add(black);
    }

public void actionPerformed(ActionEvent e) {
    Main main = (Main)e.getSource();
    if (e.getSource() == this.red)
        main.setCENTER(1);
    if(e.getSource() == this.green)
        main.setCENTER(2);
    if(e.getSource() == this.blue)
        main.setCENTER(3);
    if(e.getSource() == this.white)
        main.setCENTER(4);
    if(e.getSource() == this.black)
        main.setCENTER(5);
    }
}

The Top class is being used in Main and everytime each of the buttons is pressed the CENTER-area’s background of the Main class changes. Therefore I have programmed a small function setCENTER, which changes the background. the code for it looks like this (The code is in the Main class):

    public void setCENTER(int var){
    switch(var){
        case 1: pane.setBackground(Color.red);
        break;
        case 2: pane.setBackground(Color.green);
        break;
        case 3: pane.setBackground(Color.blue);
        break;
        case 4: pane.setBackground(Color.white);
        break;
        case 5: pane.setBackground(Color.black);
        break;
    }
}

So far the content is displayed properly (the top panel in the Main window and the Main window itself), but everytime I try to change the background by pressing on one of the 5 buttons in the top panel, I get errors and the first error points me to this line of the actionPerformed-method in the Top class:

        Main main = (Main)e.getSource();

I tried replacing it with “Main main = new Main();”. It worked, the background changes but everytime I pressed it it opened a new window (which is clearly not how it should behave).

EDIT: I forgot that Top is already a JPanel, so using another JPanel inside the class is pretty senseless. (Edited Source)

  • 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:31:50+00:00Added an answer on May 27, 2026 at 9:31 pm

    In the general case, you’ll need to follow the chain up the hierarchy, calling getParent(), until you find the parent you want. If you are looking for a specific class, through some generics magic you can write a utility to do this:

       public static <T extends Container> T findParent(Component comp, Class<T> clazz)  {
          if (comp == null)
             return null;
          if (clazz.isInstance(comp))
             return (clazz.cast(comp));
          else
             return findParent(comp.getParent(), clazz);     
       }
    

    So, if you want to find the FooPanel that is an ancester of the Button that fired the event, you’d say

    FooPanel fooPanel = findParent((Component)event.getSource(), FooPanel.class);
    fooPanel.doSomething...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got this url: http://localhost:85/WebCamClone/Home But it will only load like this: http://localhost:85/WebCamClone/index.php/Home
ok i got this problem. i have this routes: (code bit change) File:/home/dotcloud/current/config/routes.js exports.routes
I got this error when I tried to access my development site via http://localhost/home/index.html
Got this line of code here but its not working. private void Button_Click(object sender,
I got this navbar with a dropdown ul on it. <ul class=topbar-nav> <li><a href=index.php>Home</a></li>
i've got this: <div class= id=fancy_hover> <a href=home.html id=start_writer></a> <div> <p class=hidden style=display: block;>click
I have got a CSS division called home which has got certain attributes with
I have got this JavaScript code for uploading files to my server (named it
So, i've got this Zend style frontcontroller set up. Basically it just redirects every
just wondering if someone else came across this. I got this piece of code

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.