19
I ran some tests to see the difference in use, and apparently they both go off under pressure ENTER on the keyboard, as can be seen in the example below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class KeyListenerActionPerformTest extends JFrame {
private JPanel contentPane;
private JPanel panel;
private JTextField textField;
private JPanel panel2;
private JScrollPane scrollPane;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
KeyListenerActionPerformTest frame = new KeyListenerActionPerformTest();
frame.setVisible(true);
});
}
public KeyListenerActionPerformTest() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.contentPane = (JPanel) getContentPane();
this.panel = new JPanel();
this.panel.setBorder(new TitledBorder("Campo c/ Action e KeyListener"));
this.contentPane.add(this.panel, BorderLayout.CENTER);
this.textField = new JTextField(10);
this.textField.addActionListener(e -> {
textArea.append("Action triggered.\n");
});
this.textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
textArea.append("KeyEvent triggered.\n");
}
});
this.panel.add(this.textField);
this.panel2 = new JPanel();
getContentPane().add(this.panel2, BorderLayout.SOUTH);
this.textArea = new JTextArea(5, 15);
this.textArea.setLineWrap(true);
this.scrollPane = new JScrollPane(textArea);
this.panel2.add(this.scrollPane);
pack();
setLocationRelativeTo(null);
}
}
And running by pressing enter:
Therefore, I would like to leave the following questions:
- Is there any difference between using
actionPerformed
orkeyPressed
, for example in a text box? - There are differences in the way they both perform?
The
public void actionPerformed(ActionEvent e) { }
is triggered when you run an event such as clicking with the mouse or cropping text with the context menu?– Guilherme Nascimento
@Guilhermenascimento no, only enter via keyboard.
– user28595
Related: What’s the difference between Clicked Mouse and Performed Action?
– UzumakiArtanis