填充 FXML 中定义的选择框 [英] Populate Choicebox defined in FXML

查看:32
本文介绍了填充 FXML 中定义的选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 javaFX,我的问题是我有一个带有一些选择框和按钮的简单窗口.这个窗口是通过 FXML 定义的,它也与控制器类相关联.我想知道,如何用控制器类中的数据填充这个选择框,因为使用@FXML 引用这个选择框会抛出NullpointerEception

I'm learning javaFX and my problem is that I have simple window with some choicebox and button. This window is defined via FXML which is also associated with controller class. I would like to know, how to populate this choicebox with data in the controller class, because using @FXML reference to this choicebox throwsNullpointerEception

编辑 - 添加源代码FXML 代码

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0"
        prefWidth="320.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="supermarket.ManageWindowCC">
<children>
    <ChoiceBox fx:id="countChoiceBox" layoutX="44.0" layoutY="71.0" prefHeight="25.0" prefWidth="191.0"/>
    <Label layoutX="44.0" layoutY="54.0" text="To change item's count, choose one"/>
    <TextField layoutX="140.0" layoutY="129.0" prefHeight="25.0" prefWidth="24.0"/>
    <Label layoutX="123.0" layoutY="112.0" text="New count"/>
    <Button layoutX="126.1875" layoutY="171.5" mnemonicParsing="false" text="Submit"/>
</children>

Java 控制器代码:

Java controller code:

public class ManageWindowCC {
@FXML
private ChoiceBox countChoiceBox;

public void onChangeCountClick(ActionEvent actionEvent) {

    try {
        Parent root = FXMLLoader.load(getClass().getResource("ChangeCount.fxml"));
        Stage newStage = new Stage();
        newStage.setTitle("Change item's count");
        newStage.setScene(new Scene(root, 320, 240));
        newStage.show();
        countChoiceBox = new ChoiceBox();
        countChoiceBox.setItems(FXCollections.observableArrayList("One","Two","Three"));

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

感谢您的帮助和时间

推荐答案

如何解决

删除 countChoiceBox = new ChoiceBox(); 行,一切都会正常工作,假设您的应用程序的其他地方没有其他错误.

Remove the line countChoiceBox = new ChoiceBox(); and everything will work fine, assuming you have no other bugs elsewhere in your application.

程序将使用对 countChoiceBox 的引用,它是由 FXMLLoader 创建并在您的场景中设置的节点层次结构的一部分.

The program will use the reference to the countChoiceBox which is part of the node hierarchy created by the FXMLLoader and set in your scene.

发生了什么

onChangeCountClick 中加载新的 FXML 将:

Loading a new FXML in onChangeCountClick will:

  1. 创建一个新的 supermarket.ManageWindowCC 控制器.
  2. 根据 FXML 定义创建节点层次结构.
  3. 层次结构中的一个节点是 ChoiceBox.
  4. FXML 加载器自动为您创建的 ChoiceBox 将分配给 countChoiceBox 成员.
  5. 然后,您将分配给根节点的层次结构添加到新舞台上的新场景中.
  1. Create a new supermarket.ManageWindowCC controller.
  2. Create a hierarchy of Nodes based upon the FXML definition.
  3. One of the nodes in the hierarchy will be a ChoiceBox.
  4. The ChoiceBox which the FXML loader automatically creates for you will be assigned to the countChoiceBox member.
  5. You then take the hierarchy of nodes assigned to root and add it to your new Scene on your new Stage.

因此,在您加载 FXML 后,countChoiceBox 被初始化为一个由您的 FXMLLoader 实例化的空 ChoiceBox

So, after you load the FXML, countChoiceBox is initialized to an empty ChoiceBox instantiated by your FXMLLoader

到目前为止一切都很好...

This is all fine so far . . .

然后你做的是(错误地)写:

What you then do is (incorrectly) write:

countChoiceBox = new ChoiceBox();

你违反的经验法则是 =>永远不要使用 new 为标记为 @FXML 的成员赋值.

The rule of thumb you violate is => never use new to assign a value to a members tagged @FXML.

另请参阅使用 FXML 填充组合框 的相关示例(尽管该示例使用一个 ComboBox 并直接在 FXML 中填充其数据,因此它不直接适用于您的情况).

Also see the somewhat related example for populating a ComboBox using FXML (though that sample uses a ComboBox and populates its data directly in FXML, so it is not directly applicable to your situation).

这篇关于填充 FXML 中定义的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆