如何克隆JavaFX TableView?

时间:2017-03-18 02:58:22

标签: java javafx tableview

我有一个JavaFX应用程序,其主要工作是显示包含从CSV文件加载的数据的不同表。我可以毫无问题地将CSV文件读入TableViews,并将它们导出为CSV。我的问题是显示不同的TableViews。

当我从CSV加载到TableView时,我想保存该TableView以供将来处理。我还想显示一个显示新加载数据的表。我的策略是拥有一个全局TableView,调用它,添加到主VBox,并更新其内容以包含(并因此显示)我刚刚通过读取文件创建的任何TableView的元素。

这是个坏主意吗?对我来说这似乎是一个简单的概念,但我找不到一种方法来轻松“克隆”所选的TableView - 即将其内容传输到我所显示的TableView,称为table。

我在这里展示了克隆TableViews的简单尝试。我也尝试编写一个广泛的equateTable()方法,但这个方法无处可去。

//WHY DOESN'T THIS WORK?
table.setItems(reqTable.getItems());

这是代码的其余部分。感谢任何回答的人!

package FLOOR;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;

// --- Main Class
public class Main extends Application {

// --- All Tables
TableView<ObservableList<StringProperty>> reqTable = new TableView<>();
TableView<ObservableList<StringProperty>> tempTable = new TableView<>();
TableView<ObservableList<StringProperty>> ontTable = new TableView<>();

// --- Display Table
TableView<ObservableList<StringProperty>> table = new TableView<>();

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

// --- Start
@Override
public void start(Stage stage) {
    // --- Stage & Scene
    stage.setTitle("FLOOR");
    Scene scene = new Scene(new VBox(), 900, 500);
    MenuBar menuBar = new MenuBar();

    // --- VBox
    final VBox vbox = new VBox();
    vbox.setAlignment(Pos.CENTER);
    vbox.setSpacing(10);
    vbox.setPadding(new Insets(0, 10, 0, 10));
    vbox.getChildren().addAll(table);
    table.setVisible(false);

    // --- Menus 
    // --- File Menu
    // --- Import Submenu
    Menu menuFile = new Menu("File");
    Menu importMenu = new Menu("Import");
    MenuItem reqOption = new MenuItem("Requirements");
    MenuItem tempOption = new MenuItem("Templates");
    MenuItem ontOption = new MenuItem("Ontologies");
    importMenu.getItems().addAll(reqOption, tempOption, ontOption);

    //Import Requirements
    reqOption.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Select Requirements CSV");
            File file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                populateTable(reqTable, file.getAbsolutePath());
            }
            getRequirementsPage();
        }
    });

    //Import Templates
    tempOption.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Select Templates CSV");
            File file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                populateTable(tempTable, file.getAbsolutePath());
            }
            getTemplatesPage();
        }
    });

    //Import Ontologies
    ontOption.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Select Ontology CSV");
            File file = fileChooser.showOpenDialog(stage);
            if (file != null) {
                populateTable(ontTable, file.getAbsolutePath());
            }
            getOntologiesPage();
        }
    });

    //Export
    MenuItem export = new MenuItem("Export");
    export.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Save Requirements CSV");
            File file = fileChooser.showSaveDialog(stage);
            if (file != null) {
                exportTable(reqTable, file.getAbsolutePath());
            }
        }
    });

    //Exit
    MenuItem exit = new MenuItem("Exit");
    exit.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            System.exit(0);
        }
    });
    menuFile.getItems().addAll(importMenu, export, new SeparatorMenuItem(), exit);

    // --- Menu Bar
    menuBar.getMenus().addAll(menuFile);

    // --- Show FLOOR
    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
    stage.setScene(scene);
    stage.show();
}

// --- Methods
// Table Getters
private void getRequirementsPage() {
    table.getItems().clear();
    //WHY DOESN'T THIS WORK?
    table.setItems(reqTable.getItems());
    table.setVisible(true);
}
private void getTemplatesPage() {
    table.getItems().clear();
    table.setItems(tempTable.getItems());
    table.setVisible(true);
}
private void getOntologiesPage() {
    table.getItems().clear();
    table.setItems(ontTable.getItems());
    table.setVisible(true);
}

//populateTable
private void populateTable(
        final TableView<ObservableList<StringProperty>> table,
        final String filename) {
    table.getItems().clear();
    table.getColumns().clear();
    table.setPlaceholder(new Label("Loading..."));
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            CSVReader reader = new CSVReader(new FileReader(filename));
            String [] nextLine;
            int count = 1;
            while ((nextLine = reader.readNext()) != null) {
                if (count == 1) {
                    final String[] headers = nextLine;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            for (int column = 0; column < headers.length; column++) {
                                table.getColumns().add(
                                        createColumn(column, headers[column]));
                            }
                        }
                    });
                } else {
                    final String[] dataValues = nextLine;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            // Add additional columns if necessary:
                            for (int columnIndex = table.getColumns().size(); columnIndex < dataValues.length; columnIndex++) {
                                table.getColumns().add(createColumn(columnIndex, ""));
                            }
                            // Add data to table:
                            ObservableList<StringProperty> data = FXCollections
                                    .observableArrayList();
                            for (String value : dataValues) {
                                data.add(new SimpleStringProperty(value));
                            }
                            table.getItems().add(data);
                        }
                    });
                }
                count++;
            }
            reader.close();
            return null;
        }
    };
    Thread thread = new Thread(task);
    thread.setDaemon(true);
    thread.start();
}

//exportTable
private void exportTable(
        final TableView<ObservableList<StringProperty>> table,
        final String filename) {
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            CSVWriter writer = null;
            try {
                writer = new CSVWriter(new FileWriter(filename), ',');
            } catch (IOException e) {
                e.printStackTrace();
            }
            int numRows = table.getItems().size();
            int numCols = table.getColumns().size();
            String[] dataWrite = new String[numCols];
            for (int i = 0; i < numRows; i++) {
                for (int j = 0; j < numCols; j++) {
                    dataWrite[j] = table.getItems().get(i).get(j).getValue();
                }
                writer.writeNext(dataWrite);
            }
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    Thread thread = new Thread(task);
    thread.setDaemon(true);
    thread.start();
}

//createColumn
private TableColumn<ObservableList<StringProperty>, String> createColumn(
        final int columnIndex, String columnTitle) {
    TableColumn<ObservableList<StringProperty>, String> column = new TableColumn<>();
    String title;
    if (columnTitle == null || columnTitle.trim().length() == 0) {
        title = "Column " + (columnIndex + 1);
    } else {
        title = columnTitle;
    }
    column.setText(title);
    column
    .setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<StringProperty>, String>, ObservableValue<String>>() {
        @Override
        public ObservableValue<String> call(
                CellDataFeatures<ObservableList<StringProperty>, String> cellDataFeatures) {
            ObservableList<StringProperty> values = cellDataFeatures.getValue();
            if (columnIndex >= values.size()) {
                return new SimpleStringProperty("");
            } else {
                return cellDataFeatures.getValue().get(columnIndex);
            }
        }
    });
    return column;
}

}

0 个答案:

没有答案