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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:35:14+00:00 2026-05-22T21:35:14+00:00

during the setup this function is called private JPanel Defer() { defer = new

  • 0

during the setup this function is called

 private JPanel Defer()      
    {                    
        defer = new JPanel();
        defer.setLayout(new BoxLayout(defer, BoxLayout.Y_AXIS));
        JButton deferB = new JButton("Defer Staff Key Cutting");
        Color col = colour.getColour("White");
        deferB.setBackground(col);
        deferB.setActionCommand(def);
        deferB.addActionListener(this);
        defer.add(deferB); 
        return defer;
    }

It works fine and the listener reacts. The thing is I want to change the colour of the button when it is clicked and later on when I try to use the same code I have here for the colour it throws and exception claiming that deferB is not real.

As it is created in the function I know it is local but the button must exist some how for it to be shown on screen.

How can I access the button deferB to change its colour outside the function?

Also if anyone can explain how a local variable can be used throughout the program like it is being used that would be great as it does not make sense to me.

  • 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-22T21:35:15+00:00Added an answer on May 22, 2026 at 9:35 pm

    Inside the action Listener, you got to call the .getSource() method. This method returns object, so you got to cast it. Or make it a private instance variable. Then you can access it anywhere.

    (JButton)event.getSource();
    

    From what i can tell from your code, the class your working in is an action listener. Which means in your “actionPerformed(ActionEvent event)” method, you want to change the color. (because that’s the method that gets called when it is clicked. In that method, you can either do event.getSource(), or just use the variable name if you have it as an instance variable.

    private JButton button_;
    

    Let me know if you need more clarification.

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

Sidebar

Related Questions

During my application setup, the user has the option of installing a new SQL
I'm trying to setup a repository in an ActiveCollab project. During setup, it reports
In the Setup Project I have 2 web.config files: web.config - used during the
I currently have mirroring setup between three computers, principle, mirror, and witness. During the
I created a setup using Inno installer, during the setup, I made some lengthly
Greetings, I hope someone has the patience to read this. I have a setup
In the setup of my test cases, I have this code: ApplicationContext context =
I have this test [Fact] public void Get_if_item_is_not_in_cache_return_null_returns_true() { var repo = new Repository<IProduct>(
I have created a Setup Project for my Project. This project connects to a
funny, I was going to test tombstoning and concurrency, and during my simple setup,

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.