java - 使javafx choicebox根据键盘选择项目

标签 java javafx

根据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 lettersSelected 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是规范的,这个更简单的变体跳转到列表中键入字母的第一项。它还响应 EnterReturn 键。

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 ,还要考虑PrefixSelectionChoiceBoxPrefixSelectionComboBox包含在ControlsFX中.

关于java - 使javafx choicebox根据键盘选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69588828/

相关文章:

java - 抽屉式导航栏未显示

java - 如何在自定义 json 序列化器中获取属性或字段名称

JavaFx ImageView 不会在绑定(bind)到 VBox 的 fitWidthProperty 上调整大小

java - import javafx 无法解析

java - 创建忽略鼠标和按键事件的 JavaFX 透明窗口

javascript - 在webview Android中隐藏页面上的元素

java - 向 java maven 项目添加新组件

java - Java 中的多线程控制

java - 如何在同一个窗口中依次打开2个类(class)

javafx - 可折叠设置面板