I have following code:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame {
JComboBox comboBox;
MainFrame() {
this.setTitle("KfiatekApp");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
String[] animals = {"dog", "cat", "pigeon"};
comboBox = new JComboBox(animals);
comboBox.setEditable(true);
comboBox.addActionListener(_ -> System.out.println(comboBox.getSelectedItem()));
this.add(comboBox);
this.pack();
this.setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
new MainFrame();
}
}
After starting the program when I select a value with the mouse the ActionListener triggers correctly. However when I want to enter a value from the keyboard when I hit the Enter the ActionListener triggres twice (as if it treats entering a value and selecting a value as separate events?).
Sorry for my English I'm a foreigner.
Thank you in advance for your help!