Passing Arguments to ActionListener

public class start implements ActionListener {

    public void actionPerformed(ActionEvent aL) {
      method(arguments);

    }
  }

method(arguments) {
   //stuff
}

I want a JButton to tell the program to start a method using the arguments "stuff" So I put the actionListener above on the button however the actionListener does not have access to the arguments and I don't know how to give it them (I looked at the oracle docs and couldn't figure it out). I tried this:

actionPerformed(ActionEvent aL, stuff) {

and that doesn't work, I get the error

Compute.java:45: error: Compute.start is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
  public class start implements ActionListener {
         ^

And I tried putting an @Override but you can't override that according to the compiler. So, how can I give the arguments to the ActionListener or have the method outside of the button but only start working once the button is pressed, however I am using a JProgressBar within the JFrame the button is in which uses the method as it's timer thing so it would be preferable to pass the arguments to the actionListener

Answers


You can pass the arguments by constructor. But of course it means with this way you can pass the arguments only when creating the ActionListener.

public class Start implements ActionListener {

    SomeType arguments;
    public Start (SomeType arguments) {
         this.arguments = arguments;
    }

    public void actionPerformed(ActionEvent aL) {
        method(arguments);    
    }
}

Need Your Help

Is there a library to do CSS spriting automatically?

ruby-on-rails css ruby optimization

CSS Spriting can really help performance, but it's not the easiest thing to read and maintain. Are there any tools that would let me code the images individually but aggregate them up and replace ...

Trouble triggering onKey event with Android

java android key listener

I'm having difficulty getting my main View's onKey event to trigger. I'm not sure what I'm doing wrong, I have correctly implemented the onClick event but cannot seem to figure out the onKey event.