gpt4 book ai didi

JavaFX 从 SQL 查询填充组合框

转载 作者:行者123 更新时间:2023-12-02 01:33:04 26 4
gpt4 key购买 nike

我正在查询 Sql Server 并返回一个列表 - 我想使用此列表作为我的组合框的源。下面是我正在使用的代码,它运行时没有错误,但我的组合框始终为空且从未填充。这里有什么不正确的吗?

public class Controller {
private List<String> combomain;
static String getData = "QueryHEre";

void initialize() {
empnames = queryDatabase(getData);
String[] names = empnames.toArray(new String[0]);

combomain.getItems().addAll(names);
combomain.setValue("");
}

Main.java

public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 350, 275));
primaryStage.show();
}

样本.fxml

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="399.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<FlowPane hgap="10.0" layoutX="14.0" layoutY="7.0" prefHeight="30.0" prefWidth="375.0">
<children>
<Label alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="79.0" text="Employee:" />
<ComboBox id="combomain" fx:id="combomain" prefWidth="150.0" />
<Button mnemonicParsing="false" prefHeight="27.0" prefWidth="80.0" text="Get Info" />
</children>
</FlowPane>
</children>
</Pane>

最佳答案

您确定代码运行没有错误吗?您有combomain定义为List<String> ,这应该意味着 combomain.getItems()combomain.setValue("")会导致编译错误。根据您的 FXML 文件,该字段应该是 ComboBox<String> 。然而,即使你解决了这个问题,你的 initialize方法没有被调用,你的 combomain 也没有被调用被注入(inject)。来自 Introduction to FXML :

Note that, in the previous examples, the controller member fields and event handler methods were declared as public so they can be set or invoked by the loader. In practice, this is not often an issue, since a controller is generally only visible to the FXML loader that creates it. However, for developers who prefer more restricted visibility for controller fields or handler methods, the javafx.fxml.FXML annotation can be used. This annotation marks a protected or private class member as accessible to FXML. If the class being annotated is in a named module, the module containing that class must open the containing package to at least the javafx.fxml module.

自从您的 initialize方法不是public ,必须用 @FXML 注释为了让 FXMLLoader 看到并调用它。与你的 combomain 一样 field ;尽管有适当的fx:id在FXML文件中,它不会被注入(inject),因为该字段是private并且没有注释 @FXML .

您的 Controller 应如下所示:

public class Controller {

@FXML
private ComboBox<String> combomain;
static String getData = "QueryHEre";

@FXML
void initialize() {
empnames = queryDatabase(getData);
String[] names = empnames.toArray(new String[0]);

combomain.getItems().addAll(names);
combomain.setValue("");
}

}

但是,即使修复了所有问题,这仍然不起作用,因为您尚未将 Controller 类链接到 FXML 文件。正如 previous section 中提到的FXML 简介 中,实现此目的的一种方法是指定 fx:controller FXML 文件根元素中的属性:

<!-- SOME ATTRIBUTES OMITTED FOR BREVITY -->
<Pane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<!-- OMITTED FOR BREVITY -->
</Pane>

哪里sample.Controller应替换为 Controller 类的完全限定名称。如果您使用的是场景生成器,则可以通过打开“文档”部分(左侧面板)中的“ Controller ”标题 Pane 来设置 Controller 。

关于JavaFX 从 SQL 查询填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com