Re: Return values from an ActionListener?
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 12:57:45 -0700
Dave Reid wrote:
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
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;
}
}
--
Knute Johnson
email s/nospam/knute/
.
- References:
- Return values from an ActionListener?
- From: Dave Reid
- Return values from an ActionListener?
- Prev by Date: Return values from an ActionListener?
- Next by Date: Java to UML
- Previous by thread: Return values from an ActionListener?
- Next by thread: Java to UML
- Index(es):