Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem there is that your two method signatures are identical. When Java tries to figure out which method to call, it can't tell the difference between the two.</p> <p>I can think of two ways to do what you want:</p> <p>Presumably, you are registering the listeners on the buttons like <code>cancelButton.addActionListener(...)</code>. So you can either provide each button with its own anonymous inner class:</p> <pre><code>loginButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent logInEvent) { System.out.println("Button Login"); } } cancelButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent cancelEvent) { System.out.println("Cancel Login"); } } </code></pre> <p>or you can define a single actionPerformed method that checks the source of the call:</p> <pre><code>public class TheHandler implements ActionListener { JButton loginButton; JButton cancelButton; public TheHandler() { ... // Now, technically, this is bad form because you're leaking 'this'. // But as long as this will only be called after this constructor finishes // initializing, it's safe. loginButton.addActionListener(this); cancelButton.addActionListener(this); ... } ... @Override public void actionPerformed(ActionEvent evt) { if(evt.getSource() == loginButton) System.out.println("Button Login"); else if(evt.getSource() == cancelButton) System.out.println("Cancel Login"); } } </code></pre> <p>Using anonymous inner classes can sometimes be clearer, because you see the code right next to the addListener call, but it also adds a lot of boilerplate, and if you're working on a very large progect that can take a while to load, reducing the number of classes can sometimes make it load a little faster (each anonymous inner class is another thing for the JVM to load).</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.
    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