Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the getSource() and getActionCommand()

Tags:

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?

like image 803
user962206 Avatar asked Nov 21 '11 16:11

user962206


2 Answers

getActionCommand()

Returns the command string associated with this action. This string allows a "modal" component to specify one of several commands, depending on its state. For example, a single button might toggle between "show details" and "hide details". The source object and the event would be the same in each case, but the command string would identify the intended action.

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 has JTextField#setActionCommand(java.lang.String) method that you can use to set the command string used for action events generated by it.

getSource()

Returns: The object on which the Event initially occurred.

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 the ActionEvent#getSource() will give you the reference to the JTextField instance itself.

like image 28
Bhesh Gurung Avatar answered Sep 28 '22 06:09

Bhesh Gurung


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 a JButton you have the option to set the value with setActionCommand(String command) but for a JTextField 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 with java.awt.TextField.

getSource() is specified by the EventObject class that ActionEvent is a child of (via java.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.

public class Events implements ActionListener {    private static JFrame frame;     public static void main(String[] args) {      frame = new JFrame("JTextField events");     frame.getContentPane().setLayout(new FlowLayout());      JTextField field1 = new JTextField(10);     field1.addActionListener(new Events());     frame.getContentPane().add(new JLabel("Field with no action command set"));     frame.getContentPane().add(field1);      JTextField field2 = new JTextField(10);     field2.addActionListener(new Events());     field2.setActionCommand("my action command");     frame.getContentPane().add(new JLabel("Field with an action command set"));     frame.getContentPane().add(field2);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setSize(220, 150);     frame.setResizable(false);     frame.setVisible(true);   }    @Override   public void actionPerformed(ActionEvent evt) {     String cmd = evt.getActionCommand();     JOptionPane.showMessageDialog(frame, "Command: " + cmd);   }  } 
like image 189
Qwerky Avatar answered Sep 28 '22 06:09

Qwerky