import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUIEnterKey extends JFrame implements ActionListener, KeyListener
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 30;
private JLabel text1;
private JLabel text2;
private JTextField input1;
private JTextField input2;
public static void main(String[] args)
{
GUIEnterKey frame = new GUIEnterKey();
frame.setVisible(true);
}
public GUIEnterKey( )
{
setTitle("Enter Key Test");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setResizable(false);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
input1 = new JTextField();
input1.setBounds(30, 50, 150, 25);
contentPane.add(input1);
input1.addActionListener(this);
input1.addKeyListener(this);
input2 = new JTextField();
input2.setBounds(30, 100, 150, 25);
contentPane.add(input2);
input2.addActionListener(this);
input2.addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
// JButton clickedButton = (JButton) event.getSource();
}
public void keyPressed(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_ENTER)
{
JTextField currentTextField = (JTextField) event.getSource();
if (currentTextField == input1)
{
JOptionPane.showMessageDialog(null, "Enter pressed input 1");
input2.requestFocusInWindow(); //set focus
}
if (currentTextField == input2)
{
JOptionPane.showMessageDialog(null, "Enter pressed input 2");
}
}
}
public void keyReleased(KeyEvent arg0)
{
}
public void keyTyped(KeyEvent arg0)
{
}
}