But with these way, all buttons have the same listener no? So all buttons make the same code no?
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.Tushar Goel wrote: . . . 1) Same class listener : In this same class which creates GUI also implement Listener Interface . . .
That is what is wrong with that approach.Tushar Goel wrote: . . . You can use getSource() inside actionPerformed method to compare with button object or use getActionCommand() to compare with button name. . . .
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.
Tushar Goel wrote:So we need to have different listener class for each component. I will follow same practice henceforth..
No. You have lots of small simple classes.Tushar Goel wrote: . . . making that many class make the code more complex? Sorry for asking same question again..
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()).
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()).
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
|