尝试从arraylist(javafx)向tableview添加数据

mmvthczy  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(354)

我试图用observablelist中的数据填充tableview,但在运行程序时它不会显示。
以下是我的部分计划:

private TableView<Crypto> tableView = new TableView<Crypto>();
    private static ArrayList<Crypto> cryptoData = new ArrayList();
    private static ObservableList<Crypto> data = FXCollections.observableArrayList(cryptoData);

    //*******Crypto Class************
    static class Crypto{
        private SimpleStringProperty coinName, 
        coinsBought,
        costPerCoin,
        totalSpent,
        currentPrice,
        currentValue,
        profit,
        roi;

        public String getcoinName() {
            return coinName.get();
        }
        public String getCoinsBought() {
            return coinsBought.get();
        }
        public String getCostPerCoin() {
            return costPerCoin.get();
        }
        public String getTotalSpent() {
            return totalSpent.get();
        }
        public String getCurrentPrice() {
            return currentPrice.get();
        }
        public String getCurrentValue() {
            return currentValue.get();
        }
        public String getProfit() {
            return profit.get();
        }
        public String getRoi() {
            return roi.get();
        }

        Crypto(String name, String numBought, String costPerCoin, String totalSpent, String curPrice, String curValue, String profit, String roi){
            this.coinName = new SimpleStringProperty(name);
            this.coinsBought = new SimpleStringProperty(numBought);
            this.costPerCoin = new SimpleStringProperty(costPerCoin);
            this.totalSpent = new SimpleStringProperty(totalSpent);
            this.currentPrice = new SimpleStringProperty(curPrice);
            this.currentValue = new SimpleStringProperty(curValue);
            this.profit = new SimpleStringProperty(profit);
            this.roi = new SimpleStringProperty(roi);
        }

        @Override
        public String toString() {
            return ("[" + coinName.get() + ", " + coinsBought.get() + ", " + costPerCoin.get() + ", " +
                    totalSpent.get() + ", " + currentPrice.get() + ", " + currentValue.get()  + ", " +
                    profit.get() + ", " + roi.get() + "]");

        }

    }//*********END Crypto Class*************
@Override
    public void start(Stage primaryStage) {
        try {
            GridPane root = new GridPane();

            //title text
            Text titleText = new Text();
            titleText.setText("Crypto Portfolio");
            titleText.setY(600);
            titleText.setFont(Font.font("Veranda", FontWeight.BOLD, FontPosture.REGULAR,40));

            //refresh button
            Button refresh = new Button("Refresh Prices");
            refresh.setOnAction(e -> {
                //ADD button refresh
            });

            //total amount text
            Text totalDollar = new Text();
            totalDollar.setText(getTotalDollar());

            //table columns
            TableColumn coinColumn = new TableColumn("Coin");
            coinColumn.setCellValueFactory(new PropertyValueFactory<>("coinName"));

            TableColumn costColumn = new TableColumn("Cost");
            costColumn.setCellValueFactory(new PropertyValueFactory<>("totalSpent"));

            TableColumn coinBoughtColumn = new TableColumn("Coins Bought");
            coinBoughtColumn.setCellValueFactory(new PropertyValueFactory<>("coinsBought"));

            TableColumn costPerCoinColumn = new TableColumn("Cost per Coin");
            costPerCoinColumn.setCellValueFactory(new PropertyValueFactory<>("costPerCoin"));

            TableColumn currentPriceColumn = new TableColumn("Current Coin Price");
            currentPriceColumn.setCellValueFactory(new PropertyValueFactory<>("currentPrice"));

            TableColumn currentValueColumn = new TableColumn("Curren Value");
            currentValueColumn.setCellValueFactory(new PropertyValueFactory<>("currentValue"));

            TableColumn profitColumn = new TableColumn("Profit");
            profitColumn.setCellValueFactory(new PropertyValueFactory<>("profit"));

            TableColumn roiColumn = new TableColumn("ROI");
            roiColumn.setCellValueFactory(new PropertyValueFactory<>("roi"));

            tableView.setItems(data);
            tableView.getColumns().addAll(coinColumn, costColumn, coinBoughtColumn, costPerCoinColumn, currentPriceColumn, currentValueColumn, profitColumn, roiColumn);

            Scene scene = new Scene(root,1200,900);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            root.setHgap(10);
            root.setVgap(10);

            //sets gridLines visible for debug
            root.setGridLinesVisible(true);

            primaryStage.setScene(scene);
            primaryStage.setTitle("Mike's Crypto Portfolio");
            root.add(titleText, 0,0);
            root.add(refresh, 3, 0);
            root.add(tableView, 0, 1);

            primaryStage.show();

            new Thread () {
                @Override
                public void run() {
                    try {
                        readCSV("crypto.csv");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
            }.start();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }//*****************END Start***************************
}

我以为如果我添加了这个,它会工作,但我得到一个警告:无法检索propertyvaluefactory中的属性“coinname”:javafx.scene.control.cell。propertyvaluefactory@2cc2c23 使用提供的类类型:class application.main$crypto java.lang.illegalstateexception:无法从不可读的属性coinname读取
但是除了coinname之外,错误还添加了:java.lang.runtimeexception:java.lang.illegalaccessexception:class com.sun.javafx.reflect.trampoline不能访问类application.main$crypto的成员,修饰符为“public”

for(Crypto coin : cryptoData) {
            data.add(coin);
        }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题