pressing enter to perform a button's action

hi,I want to perform an action when I press a button and when I press enter (this button selected)I have no idea how to do this.thank you.
[166 byte] By [ibicema] at [2007-10-1 1:31:02]
«« New Year
»» New Year
# 1
You need to look at the Listener interfaces and Adapter classes in the java.awt.event package.
grand_poobaha at 2007-7-8 1:51:50 > top of Java-index,Security,Event Handling...
# 2

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...

}

}

}

tnguyen1973a at 2007-7-8 1:51:50 > top of Java-index,Security,Event Handling...
# 3
Don't cross post. This is a Swing related question and you posted the question in the Swing forum.
camickra at 2007-7-8 1:51:50 > top of Java-index,Security,Event Handling...