gpt4 book ai didi

JavaFX TableView 与 SceneBuilder

转载 作者:行者123 更新时间:2023-11-29 22:04:45 27 4
gpt4 key购买 nike

我有一个非常简单的类,它获取作业列表并尝试用它们填充 TableView。我在不使用 FXML 的情况下完成了这项工作,但我现在正在尝试新方法。我遇到的问题是;

tView.getItems().addAll(jobList);

返回一个空指针,但是如果我创建一个新的 TableView,则表中根本没有数据。

我相信我出错的地方是使用了在 SceneBuilder 中构造的 TableView 并且没有正确初始化它,但是我对如何执行此操作感到困惑。这是我的 InterFace 方法类;

public class InterfaceMethods {

@FXML TableView<Job> tView;

@SuppressWarnings("unchecked")
public void populateTableView(Connection connection, Stage stage) throws Exception {

Parent root;
root = FXMLLoader.load(getClass().getResource("TableViewInterface.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);

JobDataAccessor jobAccessor = new JobDataAccessor();
final String query = "SELECT * FROM progdb.adamJobs";
List<Job> jobList = jobAccessor.getJobList(connection, query);
//System.out.println(query);
//System.out.println(connection);
tView.getItems().addAll(jobList);
}
}

这是我的 MainApp 类;

public class MainApp extends Application {

private CheckUserDetails checkDetails;
private String pString, username = System.getProperty("user.name");
private InterfaceMethods iMethods;
private Connection connection;
@FXML private Button submitButton;
@FXML private PasswordField passField;
@FXML private Label warnLabel;

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainInterface.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}

@FXML
private void loginCheck() throws Exception {
checkDetails = new CheckUserDetails();
pString = new String();
pString = passField.getText();
System.out.println(pString);
if (checkDetails.checkUserDetails(pString, username) != null) {
setConnection(checkDetails.connection);
handleTableView();
} else {
warnLabel.setVisible(true);
}
}

@FXML
public void handleTableView() throws Exception {
iMethods = new InterfaceMethods();

Stage stage = (Stage) submitButton.getScene().getWindow();

connection = getConnection();

iMethods.populateTableView(connection, stage);
}

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

最后这是我的两个 FXML 文件之一,其中包含 TableView;

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.InterfaceMethods">
<children>
<TableView fx:id="tView" layoutX="17.0" layoutY="19.0" prefHeight="347.0" prefWidth="570.0">
<columns>
<TableColumn prefWidth="75.0" text="CaseNO." />
<TableColumn prefWidth="75.0" text="Case Notes" />
<TableColumn prefWidth="75.0" text="Date Created" />
<TableColumn prefWidth="75.0" text="Deadline" />
<TableColumn prefWidth="75.0" text="Priority" />
<TableColumn prefWidth="75.0" text="Completed" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>

getJobList 方法(在不同的类中);

    public List<Job> getJobList(final Connection connection, final String query) throws SQLException {
try (Statement stmnt = connection.createStatement(); ResultSet rs = stmnt.executeQuery(query);) {
jobList = new ArrayList<>();
System.out.println(connection);
System.out.println(query);
while (rs.next()) {
caseNumber = rs.getString("CaseNO");
caseNotes = rs.getString("CaseNotes");
dateCreated = rs.getString("DateCreated");
deadlineDate = rs.getString("Deadline");
prioritySetting = rs.getInt("Priority");
completedSetting = rs.getString("Completed");
job = new Job(caseNumber, caseNotes, dateCreated, deadlineDate, prioritySetting, completedSetting);
jobList.add(job);
}
return jobList;
}
}

最佳答案

You need to define the controller for your FXML.

例如,您的 FXML 部分是:-

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="288.0" prefWidth="293.0" xmlns:fx="http://javafx.com/fxml">
<children>
<TableView fx:id="tableView" layoutX="35.0" layoutY="28.0" prefHeight="200.0" prefWidth="227.0">
<columns>
<!-- Columns for table view-->
</columns>
</TableView>
</children>
</AnchorPane>

1) 为您的 FXML 页面创建一个 Controller ,例如:-

 fx:controller="com.code.Controller"

2)现在在您的 Controller 中,您可以执行以下操作:-

package com.code;

public class Controller implements Initializable {

@FXML private TableView<User> tableView;

关于JavaFX TableView 与 SceneBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32269093/

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