Results 1 to 15 of 15
Thread: ActionListener issue.
- 02-24-2011, 04:23 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
ActionListener issue.
Hi all,
Having some issues with ActionListener in Swing, I have added an actionListener to a button, and it works as expected, but if I try to change any variables, I get a "local variable is accessed from within inner class, needs to be declared Final".
Any ideas? If I want my button to, for instance, decrement an int variable, everytime it's pressed, the int can't possibly be declared as final.
Here's a snippet of my code:
Thanks!Java Code:int currentHealth = 100; JPanel mainPanel = new JPanel(); final JLabel label1 = new JLabel(Integer.toString(currentHealth)); JButton okButton = new JButton("OK"); JButton cancelButton = new JButton("CANCEL"); JButton exitButton = new JButton("Exit"); JFrame mainFrame = new JFrame("Application"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //OK Button event ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae){ currentHealth = CurrentHealth--; //ERROR HERE System.out.println("Button Pressed"); } };
- 02-24-2011, 04:39 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I cant replicate this error, can you post compilable code that shows it?
- 02-24-2011, 04:54 AM #3
From the JLS:
Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final.
It doesn't explain why. It may in another section.
- 02-24-2011, 04:58 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
- 02-24-2011, 05:00 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
The entire code:
Basically, I can only reference final variables from within the ActionListener. But if I want to modify them in the listener, obviously they can't be final, so that's the dilemma here..Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; /** * * @author kirill */ public class Main { public static void main(String[] args) { int currentHealth = 100; JPanel mainPanel = new JPanel(); final JLabel label1 = new JLabel(Integer.toString(currentHealth)); JButton okButton = new JButton("OK"); JButton cancelButton = new JButton("CANCEL"); JButton exitButton = new JButton("Exit"); JFrame mainFrame = new JFrame("Application"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //OK Button event ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae){ currentHealth = CurrentHealth--; //Won't allow this System.out.println("Button Pressed"); } }; //Cancel Button event ActionListener ac = new ActionListener(){ public void actionPerformed(ActionEvent ae){ } }; //Register actionlistener with ok button okButton.addActionListener(al); cancelButton.addActionListener(ac); mainPanel.add(okButton, BorderLayout.CENTER); mainPanel.add(label1, BorderLayout.NORTH); mainFrame.add(mainPanel); mainFrame.pack(); mainFrame.setVisible(true); } }
Thanks.
- 02-24-2011, 05:00 AM #6
- 02-24-2011, 05:00 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
- 02-24-2011, 05:01 AM #8
So don't make the variable you are trying to change local.
Yikes!
Don't stuff all your code in the main method. Java is OO and has classes for a reason.
- 02-24-2011, 05:02 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
- 02-24-2011, 05:09 AM #10
Well it won't take much to change. Move all the code you have so far into a constructor. Except the local variable you are trying to modify. Make it an instance variable instead. Then change your main method to:
Java Code:new Main();
- 02-24-2011, 05:20 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
- 02-24-2011, 05:49 AM #12
Just to be clear.
Yes that is what the main method is doing.That means i'll have to instantiate the class
If the variable is an instance variable the inner class has direct access to it. No need for a setter method.and then modify the instance variable through a setter which in turn gets called by ActionListener?
By the way
this does not work. Try the following:Java Code:currentHealth = currentHealth--;
Same goes for decrementing.Java Code:int a = 1; a = a++; System.out.println(a); int b = 1; b = ++b; System.out.println(b);
Last edited by Junky; 02-24-2011 at 05:54 AM.
- 02-24-2011, 05:58 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
- 02-24-2011, 06:03 AM #14
Just to clarify increment/decrement. When you want to use them don't assign them back to the variable. simply do:
Java Code:int a = 10; a--; --a; int b = 10; b++; ++b;
- 02-24-2011, 06:08 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
ActionListener w/ Dialog
By BariMutation in forum New To JavaReplies: 1Last Post: 12-03-2010, 04:50 PM -
ActionListener+KeyListener
By mandelbrot in forum AWT / SwingReplies: 5Last Post: 09-10-2010, 12:25 AM -
NullPointerException by ActionListener
By YouGina in forum AWT / SwingReplies: 8Last Post: 07-10-2009, 11:08 AM -
Demonstrating the ActionListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:20 PM -
How to use KeyListener and ActionListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks