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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:14:48+00:00 2026-05-12T09:14:48+00:00

OK, so if I add an ActionListener to a GUI element, and it’s the

  • 0

OK, so if I add an ActionListener to a GUI element, and it’s the only element I use that ActionListener with, does it matter which of the following lines (a,b) I use to get the checkbox selected state?

final JCheckBox checkbox = (JCheckBox)this.buildResult.get("cbDebugTick");
checkbox.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent event){               
            boolean bChecked =
            // (a) checkbox.isSelected();
            // (b) ((JCheckBox)event.getSource()).isSelected();
            model.setPrintDebugOn(bChecked);
        }
});

It makes sense to me that if I add the ActionListener object to multiple GUI elements, then I should use (b).

And in (b), is it OK to blindly cast event.getSource() to JCheckBox, since I’m the one who added the action listener, or should I program defensively and do an instanceof check?

note: this question is in the context of event listeners in general; kdgregory has some good points below specifically re: checkboxes which I had neglected to consider.

  • 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-12T09:14:48+00:00Added an answer on May 12, 2026 at 9:14 am

    in (b) to be rigourous, you should indeed do a instanceof check, but it’s not that important. I would think both these lines are fine and acceptable, though (b) would be “better code”

    Although, what is usually done in an action listener is simply call another method customized to your checkbox. So it would look like something like this:

     @Override public void actionPerformed(ActionEvent event) {                                  
        //your treatment would be in this method, where it would be acceptable to use (a)                  
        onCheckBoxActionPerformed(event)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GUI problem that I would like to get sorted out, but
I have a window with a MenuItem maddbound3 with the following ActionListener: maddbound3.addActionListener (
I have GUI created using netbeans GUI builder. I want to add there an
I am creating a Java program that uses a GUI to display a mortgage
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
I have a program with a GUI that needs to open a separate window
I have a GUI window that I've created using Netbeans. I then ported the
Basically I have a GUI that inherits from the JFrame class and has its
Im trying to use the following code to eventually make a game. The code,
I have made a java GUI program and have added a jList on that

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.