我想从计算机中取出txt file
并将其tableview
插入。文件的内容按以下方式进行;
国家,资本,人口,民主=>当然它们是tablecolumn
。项目应该是有序的。
如何取txt file
。如何将此file
转换为observableList
或这是必要的,最后如何插入tableview
。
答案 0 :(得分:1)
这是一个可以帮助你的样本。您会注意到我使用Java 8读取文件中的所有行,将它们转换为数据结构,然后将其添加到表中。希望这会有所帮助。
import java.io.File;
import java.nio.file.Files;
import java.util.Collection;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SampleApp extends Application{
public static void main(String[] args){
launch (args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Collection<CountryData> list = Files.readAllLines(new File("c:/temp/data.txt").toPath())
.stream()
.map(line -> {
String[] details = line.split(",");
CountryData cd = new CountryData();
cd.setCountry(details[0]);
cd.setCapital(details[1]);
cd.setPopulation(details[2]);
cd.setDemocracy(details[3]);
return cd;
})
.collect(Collectors.toList());
ObservableList<CountryData> details = FXCollections.observableArrayList(list);
TableView<CountryData> tableView = new TableView<>();
TableColumn<CountryData, String> col1 = new TableColumn<>();
TableColumn<CountryData, String> col2 = new TableColumn<>();
TableColumn<CountryData, String> col3 = new TableColumn<>();
TableColumn<CountryData, String> col4 = new TableColumn<>();
tableView.getColumns().addAll(col1, col2, col3, col4);
col1.setCellValueFactory(data -> data.getValue().countryProperty());
col2.setCellValueFactory(data -> data.getValue().capitalProperty());
col3.setCellValueFactory(data -> data.getValue().populationProperty());
col4.setCellValueFactory(data -> data.getValue().democracyProperty());
tableView.setItems(details);
StackPane sp = new StackPane(tableView);
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}
private class CountryData {
StringProperty country = new SimpleStringProperty();
StringProperty capital = new SimpleStringProperty();
StringProperty population = new SimpleStringProperty();
StringProperty democracy = new SimpleStringProperty();
public final StringProperty countryProperty() {
return this.country;
}
public final java.lang.String getCountry() {
return this.countryProperty().get();
}
public final void setCountry(final java.lang.String country) {
this.countryProperty().set(country);
}
public final StringProperty capitalProperty() {
return this.capital;
}
public final java.lang.String getCapital() {
return this.capitalProperty().get();
}
public final void setCapital(final java.lang.String capital) {
this.capitalProperty().set(capital);
}
public final StringProperty populationProperty() {
return this.population;
}
public final java.lang.String getPopulation() {
return this.populationProperty().get();
}
public final void setPopulation(final java.lang.String population) {
this.populationProperty().set(population);
}
public final StringProperty democracyProperty() {
return this.democracy;
}
public final java.lang.String getDemocracy() {
return this.democracyProperty().get();
}
public final void setDemocracy(final java.lang.String democracy) {
this.democracyProperty().set(democracy);
}
}
}