Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton to Fire a event  RSS feed

 
vinaya kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I want to fire an event saying that button is pressed.
ie,
I have frame with a button.
now i want to fire an event as above.
How do i do this.
thanks and Regards
vinaya
import javax.swing.*;
import java.awt.event.*;
public class JButtonTest extends JFrame
{
JButton b1;

public JButtonTest()
{
b1=new JButton("Press Me");
b1.addActionListener(new AL());
//b1.fireActionPerformed(null);
// above does not work.
/*
how do i fire the event
*/
this.getContentPane().add(b1);
show();
}

class AL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello !! u Pressed Me");
}
}

public static void main(String args[])
{
new JButtonTest();
}

}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to do this is to get all the ActionListeners that have been added to the button and then call actionPerformed on them. Here's some code showing how...



-Nate
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!