my dog learned polymorphism
The moose likes Beginning Java and the fly likes Action Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Action Listener" Watch "Action Listener" New topic

Action Listener

S. Stacey

Joined: Jul 03, 2003
Posts: 20
Hello All,
When I try and complie the below code I get the following errors:

Can someone please help me figure out how to fix my errors?
[ edited to preserve formatting of pasted compiler errors using the [code] and [/code] UBB tags -ds ]
[ December 14, 2003: Message edited by: Dirk Schreckmann ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Well first of all, unless I'm mistaken:
1) getText(textfield_1) should be changed to textfield_1.getText() -- unless you have the original function defined somewhere .. probably not though.
2) As for the action listener, it looks like cmdQuote is outside the scope of where it is referenced. If you were trying to make that QuoteLayout a nested class, then you would be able to reference cmdQuote, but not as it is. You can change Quote to implement some Quote.getQuoteLayout() or something like that so you can retrieve it, or you can correct your file to make QuoteLayout a nested class of Quote.
Hope that helps.

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
When interpretting compiler errors, it's often the first error message displayed that refers to the real culprit. In your case, the frist error message is complaining about in illegal start of an expression where your code reads public String panel inside of a method.
Note that the public keyword is used to modify the accessibility of a class member, or of an instance member, or of a constructor. If a variable is declared inside of a method, then that variable only exists inside of that method and is only accessible inside of that method. So, it isn't necessary, nor does it make sense, to modify the accessibility of a variable inside of a method. In other words, remove the keyword public from that line.
After fixing that compiler error, you'll be greeted by another - <identifier> expected on that last line of code which reads cmdQuote.addActionListener(alarmListener);
Java doesn't allow you to just call methods from code that is outside of a code block. (Here I'm considering code that defines class members and instance members to be outside of a code block.) You could take this line of code and put it in the constructor of QuoteLayout, but of course then the compiler will complain about not knowing what cmdQuote is. cmdQuote is something that you've defined in a different class. Perhaps you actually wanted to add the action listener to cmdQuote from the other class.
How's it going?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Action Listener
It's not a secret anymore!