What is getSource? and what does it return?
and what is getActionCommand() and what does it return??
I am getting confused between these two can anyone give or differentiate them to me? what's the use of getSource and getActionCommand() in UI's? specifically TextField or JTextField?
Assuming you are talking about the
ActionEvent
class, then there is a big difference between the two methods.getActionCommand()
gives you a String representing the action command. The value is component specific; for aJButton
you have the option to set the value withsetActionCommand(String command)
but for aJTextField
if you don't set this, it will automatically give you the value of the text field. According to the javadoc this is for compatability withjava.awt.TextField
.getSource()
is specified by theEventObject
class thatActionEvent
is a child of (viajava.awt.AWTEvent
). This gives you a reference to the object that the event came from.Edit:
Here is a example. There are two fields, one has an action command explicitly set, the other doesn't. Type some text into each then press enter.
I use getActionCommand() to hear buttons. I apply the setActionCommand() to each button so that I can hear whenever an event is execute with event.getActionCommand("The setActionCommand() value of the button").
I use getSource() for JRadioButtons for example. I write methods that returns each JRadioButton so in my Listener Class I can specify an action each time a new JRadioButton is pressed. So for example:
So with this I can hear button events and radioButtons events. The following are examples of how I listen each one:
In this case GUISeleccion.BOTON_ACEPTAR is a "public static final String" which is used in JButtonAceptar.setActionCommand(BOTON_ACEPTAR).
In this one, I get the source of any JRadioButton that is focused when the user hits it. guiSeleccion.getJrbDat() returns the reference to the JRadioButton that is in the class GUISeleccion (this is a Frame)
getActionCommand()
IMO, this is useful in case you a single command-component to fire different commands based on it's state, and using this method your handler can execute the right lines of code.
JTextField
hasJTextField#setActionCommand(java.lang.String)
method that you can use to set the command string used for action events generated by it.getSource()
We can use
getSource()
to identify the component and execute corresponding lines of code within an action-listener. So, we don't need to write a separate action-listener for each command-component. And since you have the reference to the component itself, you can if you need to make any changes to the component as a result of the event.If the event was generated by the
JTextField
then theActionEvent#getSource()
will give you the reference to theJTextField
instance itself.The getActionCommand() method returns an String associated with that Component set through the setActionCommand() , whereas the getSource() method returns an Object of the Object class specifying the source of the event.