

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
I want to call JButton doClick() method to simulate a click action in Java
Let us first set a JButton:
JButton btn = new JButton("DemoButton");
Now, attach action listener:
btn.addActionListener(new ClickListener());
If you have an ActionListener attached to your button it'll fire when you call the method doClick():
btn.doClick();
The following is an example to call JButton doClick() method to simulate a click action:
Example
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn = new JButton("DemoButton"); btn.addActionListener(new ClickListener()); JOptionPane.showMessageDialog(null, btn); btn.doClick(); } } class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Clicked!"); } }
Output
On clicking “DemoButton” above, the following is the output visible:
- Related Questions & Answers
- How to add action listener to JButton in Java
- How to set action command to JButton in Java
- How to call a JavaScript function on click?
- I want to resize and position a JFrame in Java. How can I achieve that?
- How to use the click() method in Action Chain class in Selenium with python?
- How to call a JavaScript function on a click event?
- How to add empty border to a JButton in Java?
- Detecting HTML <a> click-to-call support in JavaScript
- How to add Icon to JButton in Java?
- Call append() method to construct a StringBuffer object in Java
- How to declare, define and call a method in Java?
- How to call an interface method in Java?
- How to simulate scanf() method using Python?
- Java Program to pass method call as arguments to another method
- I want to teach in a school, without doing B.Ed. Will I be recruited?
Advertisements