javafx 2- tableview动态列 [英] javafx 2- tableview dynamic column

查看:132
本文介绍了javafx 2- tableview动态列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在下面的代码中建议问题.我无法在表格中填充数据. 这段代码基本上应该添加一列(col1)并添加一行,其中包含数据d1.此代码能够添​​加列,但不能添加数据.

Can someone suggest issue in below code. I am not able to populate data in table. This code basically should add one column (col1) and add one row with data d1 in it. This code is able to add column but not data.

控制器-

import java.util.ArrayList;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class FXMLTableViewController {
    @FXML private TableView tableView;

    @FXML
    private void initialize() {
        List<String> columns = new ArrayList<String>();
        columns.add("col1");
        TableColumn [] tableColumns = new TableColumn[columns.size()];     
        int columnIndex = 0;
        for(String columName : columns) {
            tableColumns[columnIndex++] = new TableColumn(columName);
        }
        tableView.getColumns().addAll(tableColumns);
        ObservableList<ObservableList> csvData = FXCollections.observableArrayList();
        ObservableList<String> row = FXCollections.observableArrayList();
        row.addAll("d1");
        csvData.add(row);
        tableView.getItems().add(csvData);

    }

    }

fxml

<?import javafx.collections.*?> 
<?import javafx.geometry.Insets?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import fxmltableview.*?>


<GridPane alignment="center" hgap="10.0" vgap="10.0" fx:controller="FXMLTableViewController"
             xmlns:fx="http://javafx.com/fxml">
     <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
          <columns>
          </columns>    
     </TableView>
</GridPane>

主类-

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class FXMLTableView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("FXML TableView Example");
        Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
        Scene myScene = new Scene(myPane);
        primaryStage.setScene(myScene);
        primaryStage.show();
    }

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

推荐答案

尝试下面的代码,它将显示与您所需的相同的

Try the below code it will showing the same as you need

List<String> columns = new ArrayList<String>();
    columns.add("col1");
    columns.add("col2");
    TableColumn [] tableColumns = new TableColumn[columns.size()];     
    int columnIndex = 0;
    for(int i=0 ; i<columns.size(); i++) {
        final int j = i;
        TableColumn col = new TableColumn(columns.get(i));
        col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                   
           public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {                                                                                             
                return new SimpleStringProperty(param.getValue().get(j).toString());                       
            }                   
        });
        tableview.getColumns().addAll(col);
    }       
    ObservableList<String> row = FXCollections.observableArrayList();
    ObservableList<String> row1 = FXCollections.observableArrayList();
    row.addAll("d1");
    row.addAll("d11");
    row1.addAll("d2");
    row1.addAll("d22");
    tableview.getItems().add(row);
    tableview.getItems().add(row1);

这篇关于javafx 2- tableview动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆