• Post Reply Bookmark Topic Watch Topic
  • New Topic

Action Listener  RSS feed

 
Antigoni Splash
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an AWTEventListener where you specify the 'EVENT_MASK' should do it
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!