可编辑JavaFX ComboBox上的MouseListener

时间:2014-12-16 07:11:36

标签: combobox javafx javafx-8

当JavaFX ComboBox设置为可编辑时,它不适用于JavaFX ComboBox。它在Non editable ComboBox上运行良好。有没有办法在可编辑的ComboBox上设置MouseEvent?

package editablecombobox;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class EditableComboBox extends Application {

@Override
public void start(Stage primaryStage) {
    ComboBox combo = new ComboBox();
    combo.setEditable(true);

    combo.setOnMousePressed(new EventHandler<MouseEvent>() {    //adding MouseEvent on ComboBox
        @Override
        public void handle(MouseEvent event) {
            System.out.println("MOUSE PRESSED!!!");
        }

    });

    StackPane root = new StackPane();
    root.getChildren().add(combo);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

setOnMousePressed无效。

1 个答案:

答案 0 :(得分:0)

我猜您只对ComboBox的可编辑TextField区域感兴趣。您需要将MouseListener添加到TextField的编辑器(ComboBox)。尝试使用:

combo.getEditor().setOnMousePressed(new EventHandler<MouseEvent>() {    
     @Override
     public void handle(MouseEvent event) {
        System.out.println("MOUSE PRESSED!!!");
     }
});