• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement actionListener

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a class with jbutton declared inside like these:

I would like to add actionlistener in my JButton's. Where must I declared these listener. How should I do these?
Many thanks and sorry for my english!
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello David,

You need to implement ActionListener Interface. There are several ways to implement ActionListener. But in either of way you need to register your component with the listener.

1) Same class listener : In this same class which creates GUI also implement Listener Interface



2) Outer class Listener: In this class which creates GUI is different with the class which implement Listener Interface


 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks

But with these way, all buttons have the same listener no? So all buttons make the same code no?

 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.

But with these way, all buttons have the same listener no? So all buttons make the same code no?



You can use getSource() inside actionPerformed method to compare with button object or use getActionCommand() to compare with button name.Then do the working on different buttons as per requirement. But i think second approach is easier.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote: . . . 1) Same class listener : In this same class which creates GUI also implement Listener Interface . . .

I do not like that method at all. You are not associating an listener object with each button object. If you have multiple buttons, you end up with a load of it‑elses. Only make a GUI component a listener if the listener is associated with that component, for example a MouseListener where the position of the mouse is relative to the component. Start looking here, and follow the other links in that thread.

I shall move this discussion to our GUIs forum because listeners are usually a GUI topic.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote: . . . You can use getSource() inside actionPerformed method to compare with button object or use getActionCommand() to compare with button name. . . .

That is what is wrong with that approach.
 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,

I must implement that when I change the JTextField and is empty the buttons are disabled.

To make these I must use a DocumenListener... but these listener I can't put in the "implements".
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
addDocumentListener is name of the method, not name of the interface you need to implement. So, you'll create a class that implements this interface and then add an instance of this class (using the addDocumentListener method) to the document of the JTextField (see JTextField.getDocument()).
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I do not like that method at all. You are not associating an listener object with each button object. If you have multiple buttons, you end up with a load of it‑elses. Only make a GUI component a listener if the listener is associated with that component, for example a MouseListener where the position of the mouse is relative to the component. Start looking here, and follow the other links in that thread.



Thanks Campbell for your suggestion. So we need to have different listener class for each component. I will follow same practice henceforth..
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:So we need to have different listener class for each component. I will follow same practice henceforth..


Not necessarily. Sometimes it might be useful to create one parametrized class for several components, if they do similar processing. Let's say that I want to put textfields on the form, and for each textfield a button that would clear its contents. I could do the following:

In this somewhat contrived example, I'm using the same listener implementations for several buttons, and still don't have to look at the e.getSource().
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome Martin has explained it better than I would have.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Martin. I have one small doubt. Say if you have several button say 10+ and each are doing separate work then dont you think making that many class make the code more complex? Sorry for asking same question again..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote: . . . making that many class make the code more complex? Sorry for asking same question again..

No. You have lots of small simple classes.

It is all right to ask the same question twice until you get it clarified.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell once again... You all guys are mentor,tutor, eveything for beginner like us.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:addDocumentListener is name of the method, not name of the interface you need to implement. So, you'll create a class that implements this interface and then add an instance of this class (using the addDocumentListener method) to the document of the JTextField (see JTextField.getDocument()).



Thanks!
I cant understand very well your explication.
Could you put me some code example.
Many thanks and sorry for the inconveniences.
 
david luis
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:addDocumentListener is name of the method, not name of the interface you need to implement. So, you'll create a class that implements this interface and then add an instance of this class (using the addDocumentListener method) to the document of the JTextField (see JTextField.getDocument()).



Sorry. I don't understand you. The class that implements the interface ActionListener not extends JTextField, so how could I instance this class and use getDocument?

Thanks
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getDocument() is a method in JTextField, which inherits it from JTextComponent. You'll call the getDocument() method on your JTextField instance and add a DocumentListener to the document thus obtained to get notified about changes to the document.

Many Swing components are based on the Model/View/Controller (MVC) pattern. This means that the GUI component just displays the data that are contained in and managed by a model, and uses the model itself to modify the data. This is also the case of the JTextField - it uses a Document instance as its model. Some components have several models. A JList, for example, has a model that manages it's data and another model that manages the selection of individual items in the list.

The component (or model) and the listener are practically always different classes. The event handling - or notification - mechanism works like this: if you're interested into some events a component generates, you'll create a new class which implements one of the interfaces that represent listener. Sometimes, you register the listener on the component itself (eg. JButton), sometimes on the model of the component (such as in this case). An important aspect is that several independent listeners can be added to a single component or model.

You might want to have a look at the Swing tutorial, or specifically at the Even Listener tutorial.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic