如何自动将数据填充到表视图中?

时间:2019-12-21 23:25:57

标签: java uitableview user-interface javafx initialization

我正在尝试在加载GUI屏幕时将数据自动填充到TableView中。目前,它可以通过单击一个按钮来工作,但这不是我想要的那样。我做了一个initialize方法,并输入了按钮方法getWallets中的相同代码,因为我发现执行此任务的工作是可行的,但是由于某些原因它无法正常工作。

请问大家看看有什么问题吗?

PS 。我没有在任何地方使用或分配过initilize方法,我不确定在哪里需要调用它或在编译程序时如何运行它

MainController类

public class MainController {
    walletDAO dao = new walletDAO();
    ArrayList<wallet> allwallets = new ArrayList<wallet>();

    @FXML VBox ConsultHR;
    @FXML private Label message;
    @FXML private ListView<ArrayList<wallet>> list;
    @FXML private TableView<wallet> tableViewData;
    @FXML private TableColumn<wallet, String> NameColumn, LocationColumn, TagColumn;

    public void getWallets(ActionEvent event) throws SQLException {
        allwallets = dao.getAllWallets();
        for(wallet w : allwallets) {
            final ObservableList<wallet> data = FXCollections.observableArrayList(w);
            //System.out.println(w);
            System.out.println("DEBUG: Test to check if there is data: >>"+data);
        }

        tableViewData.setItems(FXCollections.observableArrayList(allwallets));

        NameColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletName()));
        LocationColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletLocation()));
        TagColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletTag()));
    }

    public void initialize(URL url, ResourceBundle rb) throws SQLException {
        allwallets = dao.getAllWallets();
        for(wallet w:allwallets) {
            final ObservableList < wallet> data = FXCollections.observableArrayList(w); 
            System.out.println("DEBUG: Test to check if there is data: >>"+data);
        }

        tableViewData.setItems(FXCollections.observableArrayList(allwallets));

        NameColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletName()));
        LocationColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletLocation()));
        TagColumn.setCellValueFactory( f -> new SimpleStringProperty(f.getValue().getWalletTag()));
    }
}

Main.fxml

<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
    <Button layoutX="149.0" layoutY="251.0" mnemonicParsing="false" onAction="#getTags" prefHeight="46.0" prefWidth="82.0" text="tags" />
    <Label fx:id="message" layoutX="139.0" layoutY="209.0" prefHeight="35.0" prefWidth="101.0" />
    <ListView id="studentObservableList" fx:id="list" layoutY="209.0" prefHeight="131.0" prefWidth="139.0" />

    <TableView fx:id="tableViewData" prefHeight="200.0" prefWidth="231.0" style="-fx-border-color: red;">
        <columns>
            <TableColumn fx:id="NameColumn" prefWidth="75.0" text="Name"/>
            <TableColumn fx:id="LocationColumn" prefWidth="75.0" text="Location" />
            <TableColumn fx:id="TagColumn" prefWidth="75.0" text="Tag" />
        </columns>
    </TableView>

    <Button layoutX="251.0" layoutY="14.0" mnemonicParsing="false" onAction="#getWallets" prefHeight="56.0" prefWidth="82.0" text="wallets" />
</AnchorPane>

0 个答案:

没有答案