Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tray icon

 
Domenic Horner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, guys, my first thread here

okay i am tryign to make a tray Menu, and i have somethign like this:

to listen for my actions
as we all know that created another class file called [classname$1.class,

is there any way to make anothher class to deal with the events and implement actionlistner there instead of creating sub-classes?

i tried this but faild


and then this in my other class (that implements actionlistner)


thanks guys

 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any class can implement ActionListener, as long as they implement the actionPerformed method. The following (ugly) way is too often used:
However, the ActionListener per, well, action is a lot better.
 
Domenic Horner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but is there any way of putting it so that the class does not create a sub-class (cuz its gets messy when you have 10+ listners)
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My example code will work without any other classes. However, what do you prefer - 10 if-statements, or 10 classes that are each specialized to do only one single task?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use real words when posting to the forums. Abbreviations such as "coz" in place of "because" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic