vinay
vinay

Reputation: 1121

Javafx combobox changeListener fire action when used with keyboard arrow key

When i use arrow key in combobox (implementing changeListener) it fires action.How can I make it to work only when enter is pressed or selected with mouse .

Edit:

Basically the problem I feel is with mouse event. An action is fired when dropdown button of combobox is pressed in

CCombobox.setEditable(true);
CCombobox.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                     System.out.println("Clicked");
                }

Upvotes: 1

Views: 2604

Answers (1)

Maxoudela
Maxoudela

Reputation: 2211

The ChangeListener is registering modification made on the Selected Items. It will never give you information about the mouse or the keyboard.

What you have to do is to add some EventHandler on keyTyped (or keyPressed), and also on MouseClicked in order to catch specifically what you want. You can then react to those events:

//cb = a ComboBox
cb.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent keyEvent) {
                    if (keyEvent.getCode() == KeyCode.ENTER) {
                        //Do what you want to do
                    }
                }
            });

            cb.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent t) {
                    //Do what you want to do
                }
            });

Upvotes: 2

Related Questions