javafx - CSV 到 TableView 跳过标题 (JavaFX)

标签 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);
    }
}
}

最佳答案

你可以这样做:

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]);

关于javafx - CSV 到 TableView 跳过标题 (JavaFX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472536/

相关文章:

java - 在新值(value)出现之前先弄清楚科学按钮

java - JavaFX 中按钮的自定义碰撞形状

JavaFX:尝试使对话框出现在应用程序之前

JavaFX TableView CheckBoxTableCell 使用 boolean 值而不是 SimpleBooleanProperty

css - 表行选定的颜色更改 JavaFX

java - 如何使辅助 anchor 板扩展其尺寸以适合主 anchor 板?

java - 如何在 JavaFX 中监听 Stage 的 resize 事件?

ios - Swift NSUserDefaults TableView 多个单元格

ios - AlertView 暂停代码

ios - Swift 将 TableView 分成两部分