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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:58:35+00:00 2026-05-23T10:58:35+00:00

i have a problem implementing ActionListener on a group of 10 jButtons. Each button

  • 0

i have a problem implementing ActionListener on a group of 10 jButtons. Each button has its text property set to a digit, from 0 – 9. So jButton1 will have its text property set to 1, JButton2 will have 2 as its text, …., …. then jButton9 will have its text set to 9. When i click any of those buttons, i want to append the value of its text property to a JTextField.

The problem am getting is every time i click a button, the value of its text property is printed twice, some times thrice some even four times, it just happens randomly.

For example, if i click a button with text 4 once, i can get 44 printed in the JTextField, if i then click 7 only once again, i can end up with 4477 or even 447777. below is my code

 public class tCalculator extends JFrame implements ActionListener{

    public tCalculator(){
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btnZero.addActionListener(this);
          } 

    public void actionPerformed(ActionEvent evt) {

        String x = txtArea.getText();
        String k = evt.getActionCommand();
        String a = x + k ;
        txtArea.setText(a);

    }}

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        ActionListener actionListener = new tCalculator();
        btn1.addActionListener(actionListener);

    }   
  • 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-23T10:58:35+00:00Added an answer on May 23, 2026 at 10:58 am

    Your method btn1ActionPerformed adds another ActionListener. We do not see where it is called, but this would explain your problem. Whenever you click the button, you have one more Listener which is executed at the next click.

    Looks like this code was generated by an IDE. Remove that action there and your code should work.


    EDIT:

    1. Remove the Actions in your IDE
    2. Remove the constructor of tCalculator and put the code into the constructor of JFrame1 (below initComponents.
    ...
    initComponents();
    ActionListener actionListener = new tCalculator();
    btn1.addActionListener(actionListener);
    btn2.addActionListener(actionListener);
    ....
    

    These steps ensure that your Listener is registered exactly once per button.

    BTW:

    1. It does not make sense for tCalculator to extend JFrame. Remove that.
    2. Class names start with an upper-case letter (TCalculator). A better name could be ButtonActionListener or something like that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem implementing the operator!= in a set class deriving from an abstact
I have a problem with a command button (implementing the ICommand). I want, when
I have problem with implementing Rich Text Editor (all that i tried, e.g. TinyMCE).
I have a strange problem implementing pl/sql procedure. My procedure has four varchar input
I have problem in implementing a disjoint set ADT in C++ due to the
i have a problem implementing a list action with its own route. here is
We have a strange problem when implementing sessions with ColdFusion in IE6. After login
I have problem when I try insert some data to Informix TEXT column via
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
i have problem with autorotate on iphone i set up in all classes -

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.