#ComboBox's ActionListener triggering twice when entering values from keyboard

1 messages · Page 1 of 1 (latest)

somber compass
#

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!

versed knotBOT
#

<@&987246487241105418> please have a look, thanks.

nocturne zephyr
#

you are watching bro code?

somber compass
#

yup