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

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

问题描述

我正在学习 javaFX ,我的问题是我有一个带有一些选择框和按钮的简单窗口。该窗口通过FXML定义,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代码

EDIT - added source code FXML code

<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的引用,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.

发生了什么

中加载新的FXML onChangeCountClick 将:


  1. 创建一个新的 supermarket.ManageWindowCC 控制器。

  2. 根据FXML定义创建节点层次结构。

  3. 层次结构中的一个节点将是ChoiceBox。

  4. FXML加载程序自动为您创建的ChoiceBox 将被分配给countChoiceBox成员。

  5. 然后,您将获取分配给root的节点的层次结构并添加它在新舞台上的新场景。

  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被初始化为由您实例化的空ChoiceBox FXMLLoader

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 create a assign a value to a members tagged @FXML.

另请参阅使用FXML填充ComboBox 的相关示例(尽管如此) sample使用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中定义的Choicebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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