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
ActionEventclass, 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 aJButtonyou have the option to set the value withsetActionCommand(String command)but for aJTextFieldif 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 theEventObjectclass thatActionEventis 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.