aspose file tools*
The moose likes Java in General and the fly likes Action Listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Action Listeners" Watch "Action Listeners" New topic
Author

Action Listeners

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I am having trouble writing action listeners. I have a group of radio buttons that I have to write an action listener. I have the buttons just can't get thm to work......Can anyone help me?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Do you think an example might help?




Best regards,
Edwin Dalorzo.
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
That helped but not alot.....Here is my code...my radio butons are time zones and its suppose to change time time to the correct time zone when it is selected....I am only showing you the GUI class...ther eis a lot more class files but this is the one I am having troulbe with...

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Where do you associate your RadioButtons with the RadioListener? That's the only thing I can perceive from the code. Your radio buttons were never registered with the ActionListener, hence, they get never executed.

Could this be the problem you seek?

I hope this helps

Best regards,
Edwin Dalorzo.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
it might look like i know what i am doing but i don't.....what i have is pure guess...so any help will be appricated
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Ok, but did you check what I wrote before, that your radio buttons were never registered with the ActionListener, hence, they get never executed?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I noticed that too.....I added them but it did no good I am wondering if it has something to with the...


part of the code...i do not know
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

What Edwin is saying is that for each JRadioButton object, after it's constructed, you need to call addActionListener() on it, passing the method an instance of your ActionListener as a class. Until you do this, pressing the buttons will do nothing.

Now, let me give you some advice: make your life easier by putting all your JRadioButtons (and all other collections of nearly identical objects of the same type) into an array or Collection, and use a loop to execute the same line of code on each one, instead of repeating a line over and over. It's easy to miss one, or make a typo with all that duplication.


[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Thanks Ernest. Look, Victoria, just to make sure you did what I said. Do you have a code like this between the setActionCommand(...) and group.add(...) in your constructor?



Best regards,
Edwin Dalorzo.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
yes I do have that code....I dod not know what to do....I am lost
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, I am sorry, Victoria. I cannot infer more from your code.

Are you getting some kind of error or exception, or it simply do not come out the way you want?
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I do not get any error they just don't work.....the radio buttons if you could not tell are for different time zones....when one is selected it is supposed to change the time one the clock to that time zone....but they don't do anything.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You have some code in a method named actionPerformed. When you say that the buttons "don't do anything", does that mean the actionPerformed method is not called, or that it is called but doesn't do anything that you can see?
 
 
subject: Action Listeners