actionListener question

Hello,I got 2 action listeners and in one of them i define a variable which i need to be used in the second action listener. How can I do it? Thank u !
[172 byte] By [codingGa] at [2007-11-27 9:21:16]
# 1
could you use a class variable instead?
petes1234a at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...
# 2
but action listener won't be able to access to class variable ,no? so how to use it?
codingGa at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...
# 3
> but action listener won't be able to access to class> variable ,no? so how to use it?Yes it will.
Navy_Codera at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...
# 4

A demo:

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

class Test extends JFrame {

private int i;

private JButton button;

private JButton button2;

public Test() {

initComponents();

}

private void initComponents() {

i = 0;

button = new JButton("Increment I");

button2 = new JButton("Show Value");

button.setSize(100, 20);

button2.setSize(100, 20);

button.setLocation(5, 5);

button2.setLocation(5, 30);

this.getContentPane().setLayout(null);

this.getContentPane().add(button);

this.getContentPane().add(button2);

this.pack();

this.setSize(300, 300);

this.setLocationRelativeTo(null);

this.setTitle("Demo");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

i++;

}

});

button2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

System.out.println("i = " + i);

}

});

}

public static void main(String[] argv) {

new Test().setVisible(true);

}

}

Navy_Codera at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...
# 5
I got the following error while doing as u did in your demo:Cannot refer to a non-final variable algoName inside an inner class defined in a different methodWhy is that?Thank u very much.
codingGa at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...
# 6
What i've sometimes done to get around this to have my anonymous inner class call a method in the enclosing class. This method has access to all variables, final or not.But, I don't know what the "kosher" way of getting around this is.
petes1234a at 2007-7-12 22:15:01 > top of Java-index,Java Essentials,New To Java...