I'm still relatively new to Java/Swing programming and have a pretty
simple question about using ActionListeners.
I have a big array of stuff, and I want to send that array to a
method to do some editing of the array but using a button ActionListener.
So its no problem to send the array to the called method, but how would I
get the changed array back to the part of my program that called it? I
understand that actionListeners don't allow for returned values.
I'm sure this is likely very simple, so I appreciate the help.
dave
Knute Johnson - 27 Sep 2006 20:57 GMT
> I'm still relatively new to Java/Swing programming and have a pretty
> simple question about using ActionListeners.
[quoted text clipped - 6 lines]
>
> dave
When you pass an array to a method you are really passing a reference to
the array to the method. So you don't really have to get the array back
since you still have the reference to it. Your method can just modify
the array as I have done here. Then you can access the array again with
the existing reference. So you are correct, it is very simple, in fact
so simple that it is confusing :-).
import java.awt.*;
import java.awt.event.*;
public class test6 {
public static void main(String[] args) {
final int[] array = new int[10];
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Button b = new Button("Press Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addOne(array);
for (int i=0; i<array.length; i++)
System.out.println(array[i]);
}
});
f.add(b);
f.pack();
f.setVisible(true);
}
static void addOne(int[] array) {
for (int i=0; i<array.length; i++)
array[i] += 1;
}
}

Signature
Knute Johnson
email s/nospam/knute/