wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Calling 2 methods in an inner class using ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Calling 2 methods in an inner class using ActionListener " Watch "Calling 2 methods in an inner class using ActionListener " New topic
Author

Calling 2 methods in an inner class using ActionListener

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

I am currently writing a small program for an assignment.I have it working fine but i have a question regarding inner classes implementing ActionListener . I am basically wondering if this is ok to do,
although it works, i think it looks a bit strange?



So when the timer actionlistener is called this opens 2 methods is it ok? I haven't found a better way yet, and if this is OK to do then i will leave it, otherwise i will try to find another way to do it.

I probably should have put this in the GUI forum (maybe someone could create a JCombobox in the edit section so we can move them ourselves ;) )

Thanks
Mike
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Why do you feel it strange to call two methods inside an inner class?
What do you expect to do in an inner class?


SCJP, SCJD, SCWCD, SCBCD, SCEA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
I shall move this thread to our GUIs forum, where we usually discuss ActionListeners, where you will also find a recent thread about how many classes you should use for Listeners.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Earlier, I wrote: . . a recent thread about how many classes you should use for Listeners.
Here it is. Note I have quoted some older threads.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

thanks for the replies, maybe i didn't explain well enough what i meant. I think using one inner Class to catch two different actions isn't a problem(i actually hadn't thought about that too much as it works for this small program,possibly not a good idea for a large complex program). My question is regarding this part.

when the source equals "timer" i open 2 methods at once, i am not sure if you guys understood what i meant.





Thanks
Mike
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

mike ryan wrote:I think using one inner Class to catch two different actions isn't a problem

That's the part I don't like. Each timer should have its own listener if the actions performed for each timer are completely different.
My question is regarding this part.

when the source equals "timer" i open 2 methods at once, i am not sure if you guys understood what i meant.




If your listener needs to do a list of things when it's called, there's nothing wrong with coding it to do a list of things.

(By the way in English we "call" a Java method, we don't "open" one.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Paul Clapham wrote:. . . Each timer should have its own listener if the actions performed for each timer are completely different. . . .
That is more or less the same as in those links I quoted earlier.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Paul,

Thank you for that answer OK i will create a second listener for "timer1" then, and "call" a method sounds better, i am actually an american but learning Java from a German Correspondence course ;)


I have been in Germany for 14 years so my english skills are sometimes a bit rusty or i just can't in that moment think of the proper word so just use the first one that comes to mind.

So anyway i am glad that it is OK to call 2 methods at one, now this assignment is done and my next assignment will be a "Text Editor" program which i am looking forward to.

As far as ActionListeners go in the course i am taking they teach using one listener for instance in a GUI to catch all the actionCommands. So they want to see it like that when i do an assignment, that is the reason i currently do it that way too.But i pay attention here to how most people use them, where each button has it's own listener.

Thanks again
Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
In my opinion you should query that requirement. If they insist on it, you might consider looking for a different course.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Campbell Ritchie wrote
In my opinion you should query that requirement. If they insist on it, you might consider looking for a different course.


I agree with you but unfortunately i signed up for it and paid for it now so i want to at least get the Certificate from it lol. After this course is done i will be first learning on my own, with Dietels "Java how to program" eighth edition.
After that i am not really sure, no idea where to go then from there.There are so many different directions to in Java "JSP,Scada, JSF and so on (I don't really know what some of those actually are)".I think i want to do desktop apps, and maybe some server apps, something i think where i can work with GUI's maybe JavaFX, no idea.How did you all decide in what direction you went??

PS: i paid over 1000 Euro for this course, thinking it would be "a great course"


Thanks
Mike
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

mike ryan wrote:How did you all decide in what direction you went??

I did not. The company I am working for put me into a C++ project, then Java Swing+EJB, then JSP etc. I guess, for most people it is like that. On the way, I fell in love with Swing and JSF, but I am not getting projects to work on in the company. Also, the market will play a major hand in your decisions...
after you gain experience, you may get the confidence to go your own way...


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

hi Ranganathan Kaliyur Mannar ,

Thanks for the answer, my problem is ,at least for the moment, i am learning this just because " I always wanted to learn a programming language", so i will for now have to decide a direction to go on my own.
I will hopefully though be able to go to the programming department for 2 weeks sometime this year to see how things work there and hopefully learn from them, we have an actuall java team where i work.
Maybe with any luck i will be able to sometime work in that department. I believe they do quite a bit with JSP for our employee intranet, so maybe that is a good place to start.

Thanks
Mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Calling 2 methods in an inner class using ActionListener
 
Similar Threads
Anynomous
closing Simple message box
Anonymous Classes question
Frames
Key Listener