CSV到TableView跳过标题(JavaFX)

时间:2015-12-26 15:07:23

标签: javafx tableview

我发现TableView已经让人感到困惑,但是我现在的代码除了次要部分之外还可以正常工作:国家,资本,人口,民主都没有显示为标题,而是显示在TableView的第一行,因为在文本文件中它们也在第一行。我该如何解决这个问题?

public class TabV extends Application{

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

@Override
public void start(Stage stage) throws Exception {
    Collection<CountryData> list = Files.readAllLines(new File("c:/tableviewinputs.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> c1 = new TableColumn<>();
    TableColumn<CountryData, String> c2 = new TableColumn<>();
    TableColumn<CountryData, String> c3 = new TableColumn<>();
    TableColumn<CountryData, String> c4 = new TableColumn<>();

    tableView.getColumns().addAll(c1, c2, c3, c4);

    c1.setCellValueFactory(data -> data.getValue().countryProperty());
    c2.setCellValueFactory(data -> data.getValue().capitalProperty());
    c3.setCellValueFactory(data -> data.getValue().populationProperty());
    c4.setCellValueFactory(data -> data.getValue().democracyProperty());

    tableView.setItems(details);

    StackPane stack = new StackPane(tableView);
    Scene scene = new Scene(stack, 365, 220);
    stage.setScene(scene);
    stage.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 void setCountry(final java.lang.String country) {
        this.countryProperty().set(country);
    }

    public final StringProperty capitalProperty() {
        return this.capital;
    }


    public final void setCapital(final java.lang.String capital) {
        this.capitalProperty().set(capital);
    }

    public final StringProperty populationProperty() {
        return this.population;
    }

    public final void setPopulation(final java.lang.String population) {
        this.populationProperty().set(population);
    }

    public final StringProperty democracyProperty() {
        return this.democracy;
    }

    public final void setDemocracy(final java.lang.String democracy) {
        this.democracyProperty().set(democracy);
    }
}
}

1 个答案:

答案 0 :(得分:2)

你可以这样做:

List<String> allLines = Files.readAllLines(new File("c:/tableviewinputs.txt").toPath());

String[] headers = allLines.get(0).split(",");

Collection<CountryData> list = allLines.stream()
        .skip(1)
        .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> c1 = new TableColumn<>(headers[0]);
TableColumn<CountryData, String> c2 = new TableColumn<>(headers[1]);
TableColumn<CountryData, String> c3 = new TableColumn<>(headers[2]);
TableColumn<CountryData, String> c4 = new TableColumn<>(headers[3]);