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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:09:02+00:00 2026-06-05T09:09:02+00:00

We usually write a click handler on a button (in GWT or Swing or

  • 0

We usually write a click handler on a button (in GWT or Swing or Android) in the following way (way 1).

class A {

    public void myMethod() {

        int count = 0 ;

        Button myButton = new Button("x");
        myButton.addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
              count = 1 ;
          }

        });

    }
}

However, we can also write a different class for the click handler and implement it like the following (way 2) :

class A implements ButtonClickInterface {

    int count ;
    public void myMethod() {

        count = 0 ;

        Button myButton = new Button("x");
        myButton.addClickHandler(new MyClickHander(this));

    }

    @Override
    public void buttonClicked() {
        count = 1 ;        
    }
}

interface ButtonClickInterface {
    public void buttonClicked() ;
}

class MyClickHandler implements ClickHandler {


    ButtonClickInterface buttonClickInterface ;

    public MyClickHandler(ButtonClickInterface buttonClickInterface) {
        this.buttonClickInterface = buttonClickInterface ;
    }

    @Override
    public void onClick(ClickEvent event) {
        buttonClickInterface.buttonClicked() ;         
    }

}

Here I have to make count as a class variable. Can this have any drawbacks? Also can the above way of implementation have any other drawbacks? How would the two ways in which I have implemented the ClickHandler face off against each other in terms of complexity, coupling, number of objects created and code readability ?

  • 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-05T09:09:04+00:00Added an answer on June 5, 2026 at 9:09 am

    Your second solution is way too complex.

    I often do that on Android (pseudocode):

    class A implements OnClickListener {
    
        onCreate() {
            myButton.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.myButtonId:
                    // do something
                    break;
                // more cases
                default: 
                    Log.w(LOG_TAG, "not implemented");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually write the following to handle a right mouse click. if (e.Button ==
I usually write something like this: <mx:VBox height=100% width=155> <mx:Button label=b1 width=100%/> <mx:Buttonlabel=b2 width=100%/>
I usually write python in emacs. I'll often want to re-evaluate my file, which
When I write an algorithm I usually write down invariants in comments. For example,
When I write an SQL statement, I usually write it like this SELECT COUNT(*)
I am a Ph.D student, and I usually write articles which are later proof-read
Usually I write a control loop as follows: Loop that enters writes ints into
Usually i write my where statements as WHERE key=@0 then add a param. Now
I write a lot of .NET based plug-ins for other programs which are usually
i usually write my code with textmate. a custom command for testing the proj

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.