What is use of getSource method?

What is use of getSource method?

The getSource method is used in the actionPerformed method to determine which button was clicked….Java For Dummies Quick Reference by.

Method Description
object getSource() Returns the object on which the event occurred

Why is getSource () Used with JButton in event handling?

The getSource() method returns a reference to the source of the event that triggered the call to actionPerformed. The conditional will be true if the source object is an instance of JButton, i.e., if the source object is an object from the JButton class (or a subclass of JButton).

What will be the return type of getSource () method?

The getSource() Method Since the return type of getSource() is Object (the class at the very top of the class hierarchy) use a type cast with it: // listener method public void stateChanged( ChangeEvent evt ) { JSlider source; source = (JSlider)evt. getSource(); . . . . }

What is actionPerformed method in Java?

ActionListener in Java is a class that is responsible for handling all action events such as when the user clicks on a component. Mostly, action listeners are used for JButtons. It can also be used separately from the class by creating a new class that implements it.

What is the difference between getSource and getActionCommand?

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

Which method is used to get source of event?

Which of these methods can be used to determine the type of event? Explanation: getID() can be used to determine the type of an event.

How is event handling done in Java?

Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events.

Is actionPerformed a method?

setText(“Button Clicked ” + numClicks + ” times”); Now, when the user clicks the Button b, the button fires an action event which invokes the action listener’s actionPerformed method….The Action Listener API.

Method Purpose
actionPerformed(actionEvent) Called just after the user performs an action.

Is java a click?

The Java ActionListener is notified whenever you click on the button or menu item. It is notified against ActionEvent. The ActionListener interface is found in java. awt.

What is the use of getSource () and getActionCommand () method?

If the event was generated by the JTextField then the ActionEvent#getSource() will give you the reference to the JTextField instance itself. 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.

Which method is used to process mouse click?

A MouseEvent object is passed to every MouseListener or MouseAdapter object which is registered to receive the “interesting” mouse events using the component’s addMouseListener method. ( MouseAdapter objects implement the MouseListener interface.) Each such listener object gets a MouseEvent containing the mouse event.

Which of the following is the best definition for event handling?

Event handling is the receipt of an event at some event handler from an event producer and subsequent processes.

Back To Top