File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT / JFace and the fly likes JButton to Fire a event Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JButton to Fire a event" Watch "JButton to Fire a event" New topic
Author

JButton to Fire a event

vinaya kumar
Ranch Hand

Joined: Jan 20, 2002
Posts: 70
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

Joined: Oct 18, 2000
Posts: 4120

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
 
subject: JButton to Fire a event
 
Threads others viewed
Can I call System.exit() from an applet ? If so, how ?
Can't get an ActionListener to change a JLabel text
Trouble with my inner class
button mnemonic
mouse event
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture