• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Action Listener

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the ways to listen to events when creating a java gui? Can i have a generic action listener for all events and don't add to every button an action listener? Also what is the different between implementing an action listener and not?!

Thanks in advance,
a newbie in java and java ranch!
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an AWTEventListener where you specify the 'EVENT_MASK' should do it
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not familiar with Michael Dunn's suggestion, but I can offer this...

I usually just have my main GUI class extend JPanel and implement action listener, or mouseListener. By extending JPanel you can easily add your GUI to either a JFrame or a JApplet.

Any component that is added to the JPanel can have an action listener on it. for example, the contructor for my JPanel class might call a buildGUI method which includes the following code for a JButton.

JButton jb1 = new JButton("Action1");
jb1.addActionListener(this);

Then my GUI will have a single actionPerformed method, which will use if...then blocks for each possible action command. e,g,




p.s. sorry, please ignore, I didn't read the question carefully, my suggestion is what you have said you don't want to do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic