th eeasiest way to do this is to implement an ActionListener 9which then, you must implement the actionPerformed(ActionEvent e) method
example:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Demo extends JFrame implements ActionListener{
private JButton btnExit = null;
private JButton btnBrowse = null;
public Demo(){
btnExit = new JButton("Exit");
// tell what action listener to use, which is this class (since we implements
// the ActionListener
btnExit(AddActionListener(this);
getContentPane().add(btnExit, "South");
btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
}
// This method is the implementation of the ActionListener interface
public void actionPerformed(ActionEvent e){
Object o = event.getSource(); // return the object that trigger the action event
if (o == btnExit){ // the object triggered is the exit button
this.dispose(); // dispose the JFrame
System.exit(0); // wuit the application
}
else if (o == btnBrowse){
JFileChooser fc = new JFileChooser();
// etc...
}
}
}