java JavaFX 组合框上的“没有选择项”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25877323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:50:31  来源:igfitidea点击:

"No select item" on JavaFX combobox?

javajavafxjavafx-2javafx-8

提问by Mateus Viccari

What is the correct way to put an item which value is null, inside a ComboBox? I tried using myComboBox.getItems().add(null);, and it works, but whenever the user selects this value on the combo box, an exception is thrown on the console:

在 ComboBox 中放置值为 null 的项目的正确方法是什么?我尝试使用myComboBox.getItems().add(null);,并且它有效,但是每当用户在组合框中选择此值时,控制台上就会抛出异常:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)

So i think maybe this is not the correct way of doing that. Any clues?

所以我认为这可能不是正确的做法。有什么线索吗?

回答by Tarje

In my experience, this is a problem introduced in Java 8u20. In Java 8u05 and 8u11, as well as JavaFX 2.x, you could add nullto the list of items, and selecting this item behaved as expected. In Java 8u20 you will get a java.lang.IndexOutOfBoundsExceptionwhenever selecting the nullvalue.

以我的经验,这是Java 8u20中引入的问题。在 Java 8u05 和 8u11 以及 JavaFX 2.x 中,您可以添加null到项目列表中,并按预期选择该项目。在 Java 8u20 中,java.lang.IndexOutOfBoundsException无论何时选择该null值,您都会得到一个。

Benjamin Gale: you will have to use Java 8u20, select an item in the ComboBox, and then select the nullvalue to see the issue.

Benjamin Gale:您必须使用 Java 8u20,在 ComboBox 中选择一个项目,然后选择null值以查看问题。

At the current time, the best option seems to be to create a special null-object and label it appropriately, as already mentioned.

目前,最好的选择似乎是创建一个特殊的null-object 并适当地标记它,如前所述。

Alternatively, if you can get by with using a ChoiceBox instead, I think you will find that it works the way you want.

或者,如果您可以使用 ChoiceBox 来代替,我想您会发现它以您想要的方式工作。

回答by Benjamin Gale

I think it must be something with your code that you are not showing us as the SSCCEbelow works fine and doesn't throw any exceptions.

我认为一定是您的代码没有向我们展示,因为下面的SSCCE工作正常并且不会引发任何异常。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NullComboBoxApp extends Application {

    @Override
    public void start(Stage primaryStage) {

        ComboBox<String> cb = new ComboBox<>(FXCollections.observableArrayList(
                "Option 1",
                "Option 2",
                "Option 3"
        ));

        cb.getItems().add(null);

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

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

        primaryStage.setTitle("Null ComboBox Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Although this actually works, quite why you'd want your users to be able to select nullis beyond me...is this actually the behaviour you want? I suspect you really want to be able to clear the current selection, something like:

虽然这确实有效,但为什么您希望您的用户能够选择null超出了我的范围……这实际上是您想要的行为吗?我怀疑您真的希望能够清除当前选择,例如:

cb.getSelectionModel().clearSelection();