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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:32:12+00:00 2026-06-14T02:32:12+00:00

I have a simple GUI which contains: a push button. Two radio buttons Now

  • 0

I have a simple GUI which contains:

  • a push button.
  • Two radio buttons

Now I want to listen to each one of these buttons. What I do is something like that:

public class TestApp implements ActionListener {

    private JFrame frame;
    private JButton btn;
    private JRadioButton rdb1;
    private JRadioButton rdb2; 

    public static void main(String[] args) { /*....*/ }

    private void initialize() {
       //Each time I add a button, I add it to the listener:
       btn = new JButton("Button");
       btn.addActionListener(this);
       //..
       rdb1 = new JRadioButton("Value1");
       rdb1.addActionListener(this);
       //And so on...
    }

    //The ActionEvents  
    public void actionPerformed(ActionEvent e) {
       if(e.getSource()==btn)
       //...
       if(e.getSource()==rdb1)
       //...        
    }
}

Now I want to know if this considered a good/bad style?

  • 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-14T02:32:13+00:00Added an answer on June 14, 2026 at 2:32 am

    Unless the listener is a very long method, I personally prefer the anonymous class pattern:

            final JButton btn = new JButton("Button");
            final JRadioButton rdb1 = new JRadioButton("Value1");
            final ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    if (e.getSource() == btn) {
                        //...
                    } else if (e.getSource() == rdb1) {
                        //...        
                    }
                }
            };
            btn.addActionListener(listener);
            rdb1.addActionListener(listener);
    

    or even better:

        btn.addActionListener(new ActionListener (){
             public void actionPerformed(ActionEvent e) {      
                 // btn handling code
                 }
        });
        rdb1.addActionListener(new ActionListener (){
             public void actionPerformed(ActionEvent e) {      
                 // rdb1 handling code
                 }
        });
    

    The pattern you are using allows other class to set the class TestApp to be set as a listener by other classes – unless this is intended, it is not a good practice.

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

Sidebar

Related Questions

I am creating a simple GUI in C++ which have few buttons in it.
I have a simple GUI program that creates a new window (which contains a
i have a simple swing gui where there are two text fields one for
I have a simple struct which contains GUI controls in an application I'm working
I'm designing a simple GUI. I have Widgets, which have children and one parent.
I have created a GUI for starting a Thread which does something very simple.
Im working now on simple gui appllication in Java/C++ and CORBA. I want my
I have created a simple GUI engine which I plan to use in a
Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which
I have created a simple GUI which includes a JTable. This table may be

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.