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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:11+00:00 2026-05-14T02:34:11+00:00

I have a big table containing a button in each cell. These buttons are

  • 0

I have a big table containing a button in each cell. These buttons are very similar and do almost the same. If I add an action listener to every button in this way:

tmp.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent evt) {
      proposition = proposition + action;
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            updatePropositionPanel();
         }
      });
   }
});

Actually, every action listener differ from all others by the value of the action. proposition and updatePropositionPanel are a field and a method of the class.

  1. First i thought that I can make it shorter if I do not use inner classes. So, I decided to program a new ActionListener class. But than I realized that in this case “proposition” will not be visible to the instances of this class.

  2. Then I decided to add the actionPerformed method to the current class and do that: addActionListener(this). But than I realized that I do not know how give arguments to the actionPerformed method.

So, how does it work. Can I add an action listener in a short and elegent way?

ADDED:

I liked the idea to program an inner class with a constructor which can take some arguments and actioPerformed method, which can use arguments given in the constructor. I started to do so and then realized that it creates a conflicts with other inner anonymous classes (used like in the above given code). So, I think I will create another class (not inner one).

  • 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-14T02:34:11+00:00Added an answer on May 14, 2026 at 2:34 am

    You can create your own class and pass the data into the constructor. For example

    public class MyActionListener
    {
        private int proposition;
        private MyOtherClass moc;
    
        public MyActionListener(int proposition, MyOtherClass moc) {
            this.proposition = proposition;
            this.moc = moc;
        }
    
        public void actionPerformed(ActionEvent evt) {
            proposition += moc.action;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    moc.updatePropositionPanel();
                }
            });
        }
    }
    

    Then you can add it as normal, passing whatever arguments you like to the constructor:

    tmp.addActionListener( new MyActionListener(proposition, this) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pygtk Table with 16 squares, each of them containing a label.
i have big table with many html items such as select, textbox, radio-button, checkbox.
hello i have a big table of products in each tree there is a
I have a table with two large cells, the left cell containing two drop-downs
I have a big json object containing cell data from a sample spreadsheet that
I have big records to copy values from table B to table A. Currently
I have a big problem. I want to extract text from html table that
I have a MySQL table which has a product_id field (big integer) 1102330008 1102330025
so I have this HTML table with a bunch of big numbers in it
I have created a static table cell in a .xib, however when it is

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.