这个问题乍一看似乎很简单,但我已经困扰了几天了。
所以,我的问题是,当组合框选择打开并且单击鼠标来选择选项时,我想检测鼠标单击和选择。
因此,它应该做的是检测所选内容上的鼠标单击并获取所选值:
PS:我的 ComboBox 的代码可以在这里看到:单击时选择 JavaFX 可编辑组合框文本 https://stackoverflow.com/questions/39830833/select-javafx-editable-combobox-text-on-click
请随时提出其他问题。
只需使用单元工厂,并向单元注册一个处理程序:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ComboBoxMouseClickOnCell extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox<String> combo = new ComboBox<>();
combo.getItems().addAll("One", "Two", "Three");
combo.setCellFactory(lv -> {
ListCell<String> cell = new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : item);
}
};
cell.setOnMousePressed(e -> {
if (! cell.isEmpty()) {
System.out.println("Click on "+cell.getItem());
}
});
return cell ;
});
Scene scene = new Scene(new StackPane(combo), 300, 180);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)