根据https://wiki.openjdk.java.net/display/OpenJFX/ChoiceBox+User+Experience+Documentation
Any Alpha-numeric Keys
应该Jumps to the first item in the list with that letter or letters
在 Selected state
但是,情况似乎并非如此,选择小部件时键入似乎不会执行任何操作。
这是我的最小可重现示例:
package com.techniqab.testchoicebox;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.net.URL;
import java.nio.file.Paths;
public class TestchoiceboxApplication extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
URL url = Paths.get("./src/main/resources/sample.fxml").toUri().toURL();
Parent root = FXMLLoader.load(url);
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
var cb = new ChoiceBox();
cb.getItems().add("a");
cb.getItems().add("b");
((Pane) root).getChildren().add(cb);
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.techniqab.testchoicebox.TestchoiceboxApplication"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>
module testchoicebox {
requires javafx.controls;
requires javafx.graphics;
requires javafx.fxml;
exports com.techniqab.testchoicebox;
opens com.techniqab.testchoicebox to javafx.graphics;
}
最佳答案
而ChoiceBox User Experience Documentation是规范的,这个更简单的变体跳转到列表中键入字母的第一项。它还响应 Enter 和 Return 键。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
/**
* https://stackoverflow.com/a/69591397/230513
*/
public class KeyTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Key Test");
ObservableList<String> items = FXCollections.observableArrayList(
"a10", "b42", "c33", "a11", "b22", "c333", "d0");
FXCollections.sort(items);
ChoiceBox cb = new ChoiceBox(items);
EventHandler<KeyEvent> keyEventHandler = (var e) -> {
if (e.getCode() == KeyCode.ENTER) {
return;
}
e.consume();
for (var item : cb.getItems()) {
if (item.toString().startsWith(e.getCharacter())) {
cb.getSelectionModel().select(item);
cb.show();
break;
}
}
};
cb.setOnKeyTyped(keyEventHandler);
StackPane root = new StackPane();
StackPane.setMargin(cb, new Insets(16, 16, 16, 16));
root.getChildren().add(cb);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
对于多键前缀选择,如所述here ,还要考虑PrefixSelectionChoiceBox
或PrefixSelectionComboBox
包含在ControlsFX中.
关于java - 使javafx choicebox根据键盘选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69588828/