Register a Static JButton

I have a JButton that is declared static in my class decalarations. When I try to register the button in a static method using:

saveDataButton.addActionListener(this);

I get the error message:

non-static variable this cannot be referenced from a static context saveDataButton.addActionListener(this);

^

To get around this error, I tried to use:

Action x=saveDataButton.getAction();

saveDataButton.addActionListener(x);

I didn't get the compile error, but the button doesn't fire with this code.

Can you provide assistance with registering this as 'static' or a way to get around the above problem?

Any assistance would be greatly appreciated.

Scott

[735 byte] By [scott7379] at [2007-9-26 3:00:13]
# 1

please post your code of:

1) your static field declaration, and the name of the class it is defined in

2) the method that do try to access this access field but seems to not compile, give the enclosing class name for that one too.

general remark:

to avoid having problems, like have a dynamic variable that hide a static one, whenever accessing a static variable, access it using the class name, like the following:MyClass.myStaticField

remu at 2007-6-29 10:57:03 > top of Java-index,Archived Forums,Swing...
# 2
Thanks!! The general information you provided was enough to send me in the right direction. I got the buttons to handle the events. Thanks!
scott7379 at 2007-6-29 10:57:03 > top of Java-index,Archived Forums,Swing...
# 3
Thanks!! The general information you provided was enough to send me in the right direction. I got the buttons to handle the events. Thanks!
scott7379 at 2007-6-29 10:57:03 > top of Java-index,Archived Forums,Swing...