JavaFX Dynamic TableView无法显示

时间:2015-03-11 13:31:35

标签: java javafx tableview

所以这是我的问题,我试图创建一个tableView,显示文件中的所有数据。但问题是内容没有显示我已经使它在控制台中打印出tableview,并且我可以告诉它获取了所有数据,但它没有显示在tableView上。

import java.io.IOException;
import java.util.Arrays;

import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class InputAndOutputSelectionController {
TrainingDataFileControl trainingData = new TrainingDataFileControl();
int colsNum = trainingData.getColNum();
int rowNum = trainingData.getRowNum();
@FXML
private TableView<String[]> trainingDataTable;

@SuppressWarnings({ "unused", "unchecked", "rawtypes" })
@FXML
private void initialize() {
    //Does not work HELP not showing the data but data is in there
    System.out.println("Input And Output Screen Loaded");
    //Test for stackpane 
    StackPane root = new StackPane();
    float[][] floatTrainingDataArray = trainingData.getTraingData(); //Gets a float array of all the training data
    String[][] stringTrainingDataArray = convertArraytoString(floatTrainingDataArray); //Converts array
    ObservableList<String[]> TrainingDataFile = FXCollections.observableArrayList(); //Sets a observable list
    TrainingDataFile.addAll(Arrays.asList(stringTrainingDataArray)); //Put the string[][] array into a list
    trainingDataTable = new TableView(); //create new tableView
    for (int i = 0; i < stringTrainingDataArray.length; i++) { //Makes  the table columns
        TableColumn tableCol = new TableColumn(String.valueOf(i));//Creates a table column with i as it's name
        final int colNo = i; //Makes a final int called colNo and gets the value off i
        //tableCol sets the cell  factory shows what kind of data is going into to it
        tableCol.setCellValueFactory(new Callback<CellDataFeatures<String[], String>, ObservableValue<String>>() {
            @Override
            //Creates the table column
            public ObservableValue<String> call(CellDataFeatures<String[], String> cellData) {
                return new SimpleStringProperty((cellData.getValue()[colNo]));
            }
        });
        trainingDataTable.getColumns().add(tableCol); //Gets the columns adds them to the table
    }
    trainingDataTable.setItems(TrainingDataFile); //Displays all the data in list
    root.getChildren().add(trainingDataTable);

}
//Used to convert float[][] array to string[][]array
public String[][] convertArraytoString(float[][] floatArray) {
    String[][] StringArray = new String[rowNum][colsNum];
    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < colsNum; j++) {
            StringArray[i][j] = Float.toString(floatArray[i][j]);
        }
    }

    return StringArray;
}

@FXML
void goToImportData(ActionEvent event) throws IOException {
    Stage stage = new Stage();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("h.fxml"));
    CSVInputFile controller = new CSVInputFile();
    loader.setController(controller);
    Parent root = (Parent) loader.load();
    stage.setScene(new Scene(root));
    stage.show();
    VistaNavigator.loadVista(VistaNavigator.ImportData);
}

@FXML
void goToAdvancedInputAndOutputSelection(ActionEvent event) {
    VistaNavigator
            .loadVista(VistaNavigator.AdvancedInputAndOutputSelection);
}

@FXML
void goBasicAlgorithmSelection(ActionEvent event) {
    VistaNavigator.loadVista(VistaNavigator.BasicAlgorithmSelection);
}

}

由于我不知道有多少列需要动态创建它们,希望你们能帮忙。

0 个答案:

没有答案