当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无效。
答案 0 :(得分:0)
我猜您只对ComboBox
的可编辑TextField区域感兴趣。您需要将MouseListener
添加到TextField
的编辑器(ComboBox
)。尝试使用:
combo.getEditor().setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("MOUSE PRESSED!!!");
}
});