我对 Java 很陌生,我在 Internet 上搜索一种将外部 csv 加载到 JavaFX TableView 的简单方法。我能够将 CSV 解析为一个数组,但我现在不知道如何处理它。然后我在玩 DataFX 库。但是再次无法将解析的 csv 传递到我的表中。我想我不太了解 ObservableLists 我认为这是必要的吗?你知道一个好的教程,或者你能解释一下解析文件后的下一步是什么吗?谢谢
编辑:这就是我所做的
import javafx.application.Application;
import javafx.scene.SceneBuilder;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import org.javafxdata.datasources.reader.FileSource;
import org.javafxdata.datasources.provider.CSVDataSource;
public class CSVTableSample extends Application {
@SuppressWarnings("unchecked")
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Test App");
// Just loading the file...
FileSource fs = new FileSource("test.csv");
// Now creating my datasource
CSVDataSource dataSource = new CSVDataSource(
fs, "order-id", "order-item-id");
@SuppressWarnings("rawtypes")
TableView table1 = new TableView();
TableColumn<?, ?> orderCol = dataSource.getNamedColumn("order-id");
TableColumn<?, ?> itemCol = dataSource.getNamedColumn("order-item-id");
table1.getColumns().addAll(orderCol, itemCol);
table1.setItems(dataSource);
stage.setScene(SceneBuilder.create().root(table1).build());
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
eclipse 说 for table1.setItems(dataSource);
TableView 类型中的方法 setItems(ObservableList) 不适用于参数(CSVDataSource)