Note that there are some explanatory texts on larger screens.

plurals
  1. POCan addition of an ActionListener be short? Can I add arguments to the actionPerformed?
    primarykey
    data
    text
    <p>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:</p> <pre><code>tmp.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt) { proposition = proposition + action; SwingUtilities.invokeLater(new Runnable() { public void run() { updatePropositionPanel(); } }); } }); </code></pre> <p>Actually, every action listener differ from all others by the value of the <code>action</code>. <code>proposition</code> and <code>updatePropositionPanel</code> are a field and a method of the class.</p> <ol> <li><p>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.</p></li> <li><p>Then I decided to add the actionPerformed method to the current class and do that: <code>addActionListener(this)</code>. But than I realized that I do not know how give arguments to the actionPerformed method.</p></li> </ol> <p>So, how does it work. Can I add an action listener in a short and elegent way?</p> <p><strong>ADDED:</strong></p> <p>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).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload