JavaFX TableView 用相同的值填充列 3 次

问题描述 投票:0回答:1

我有一段用于设置 TableView 的代码

    @Override
    public void initialize(URL location, ResourceBundle resources){
        if(App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex).getAccounttype().equalsIgnoreCase("tmb")||App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex).getAccounttype().equalsIgnoreCase("gold")){
            Checking workchecking = (Checking) App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex);
            String Balance = Double.toString(workchecking.getBalance());
            String Dateopened = workchecking.getDateopened().toString();
            String Savingsid = workchecking.getSavingsaccountid();
            ObservableList<String> list = FXCollections.observableArrayList();
            list.addAll(Balance,Dateopened,Savingsid);
            tablebox.setItems(list);
            TableColumn<String,String> column1 = new TableColumn<String,String>("Balance");
            column1.setCellValueFactory(cellData -> new SimpleStringProperty(Balance));
            TableColumn<String,String> column2= new TableColumn<>("Date Opened");
            column2.setCellValueFactory(cellData -> new SimpleStringProperty(Dateopened));
            TableColumn<String,String> column3 = new TableColumn<>("Savings ID");
            column3.setCellValueFactory(cellData -> new SimpleStringProperty(Savingsid));
            tablebox.getColumns().setAll(column1,column2,column3);
        }
    }

这会产生以下 TableView:

那么你知道我在这里做错了什么吗?我还尝试用

cellData.getValue()
代替 SimpleStringProperty 中的 Balance、DateOpened 和 SavingsID,得到相同的结果。我希望数据仅显示在第一行..

java javafx tableview
1个回答
0
投票

用这个解决了:

    public void initialize(URL location, ResourceBundle resources){
        if(App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex).getAccounttype().equalsIgnoreCase("tmb")||App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex).getAccounttype().equalsIgnoreCase("gold")){
            Checking workchecking = (Checking) App.Customers.get(App.currentcustomerindex).getAccounts().get(App.currentaccountindex);
            String Balance = Double.toString(workchecking.getBalance());
            String Dateopened = workchecking.getDateopened().toString();
            String Savingsid = workchecking.getSavingsaccountid();
            ObservableList<Checking> list = FXCollections.observableArrayList();
            list.addAll(workchecking);
            tablebox.setItems(list);
            TableColumn<Checking,Double> column1 = new TableColumn<>("Balance");
            column1.setCellValueFactory(new PropertyValueFactory<>("balance"));
            TableColumn<Checking,LocalDate> column2= new TableColumn<>("Date Opened");
            column2.setCellValueFactory(new PropertyValueFactory<>("dateopened"));
            TableColumn<Checking,String> column3 = new TableColumn<>("Savings ID");
            column3.setCellValueFactory(new PropertyValueFactory<>("savingsaccountid"));
            tablebox.getColumns().setAll(column1,column2,column3);
        }
    }

PropertyValueFactory 中的值是 Checking 类中相关属性的名称

© www.soinside.com 2019 - 2024. All rights reserved.