有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从中读取数据后,java无法将数据写入JavaFXML中的TableView。jsonfile

我的问题是,我有一个List,它是一个ArrayList,它被我的对象Daten填充,我保存在一个。json文件

    PopUp pop = new PopUp();
    Gson gson = new GsonBuilder().create();
    JsonWriter writer = new JsonWriter(new FileWriter(new File(System.getProperty("user.home"), "/Wirtschaft.json")));
    gson.toJson(allEntries, List.class, writer);
    try {
        writer.flush();
        writer.close();
        pop.show("Saved!");
    } catch (IOException e) {
        pop.show("Trying to save data failed!");
    }

为了再次使用Daten列表,我阅读了。json文件并将其保存在List<Daten>

Gson gson = new GsonBuilder().create();
    List<Daten> allSaves = new ArrayList<>();
    if (new File(System.getProperty("user.home"), "/Wirtschaft.json").exists()) {
        JsonReader jReader = new JsonReader(new FileReader(new File(System.getProperty("user.home"), "/Wirtschaft.json")));

        BufferedReader br = new BufferedReader(new FileReader(new File(System.getProperty("user.home"), "/Wirtschaft.json")));
        if (br.readLine() != null) {
            allSaves = gson.fromJson(jReader, List.class);
        }
        br.close();
        jReader.close();
    }

    return allSaves;

现在我想在TableView中显示这个List,如下所示:

ObservableList<Daten> listEntries = FXCollections.observableArrayList(Daten.readData());

    columnGewicht.setCellValueFactory(new PropertyValueFactory<>("gewicht"));
    columnPreis.setCellValueFactory(new PropertyValueFactory<>("preisProStueck"));
    columnGewinn.setCellValueFactory(new PropertyValueFactory<>("gewinn"));
    columnEB.setCellValueFactory(new PropertyValueFactory<>("eb"));
    columnAKK.setCellValueFactory(new PropertyValueFactory<>("akk"));
    columnSB.setCellValueFactory(new PropertyValueFactory<>("sb"));
    columnGK.setCellValueFactory(new PropertyValueFactory<>("gk"));
    columnBoni.setCellValueFactory(new PropertyValueFactory<>("boni"));

    table.setItems(listEntries);

问题是,当我使用上面的代码时,TableView仍然是空的,但是如果我不使用我在上面的文件中编写的Daten,它会工作并显示TableView中的所有内容,即使我选择了相同的数字等等:

List<Daten> list = new ArrayList<>();
    list.add(new Daten(100, 6, 421, 3, 4, 1, 6, 0));
    ObservableList<Daten> listEntries = FXCollections.observableArrayList(list);

    columnGewicht.setCellValueFactory(new PropertyValueFactory<>("gewicht"));
    columnPreis.setCellValueFactory(new PropertyValueFactory<>("preisProStueck"));
    columnGewinn.setCellValueFactory(new PropertyValueFactory<>("gewinn"));
    columnEB.setCellValueFactory(new PropertyValueFactory<>("eb"));
    columnAKK.setCellValueFactory(new PropertyValueFactory<>("akk"));
    columnSB.setCellValueFactory(new PropertyValueFactory<>("sb"));
    columnGK.setCellValueFactory(new PropertyValueFactory<>("gk"));
    columnBoni.setCellValueFactory(new PropertyValueFactory<>("boni"));

    table.setItems(listEntries);

如何修复TableView不显示我从文件中读取的数据的问题?就像TableView不会有什么问题,如果这个方法有效的话。。。不幸的是,我一无所知

提前谢谢

编辑:

下面是Daten的类:

public class Daten {

private Double gewicht;
private Double preisProStueck;
private Double gewinn;
private Double eb;
private Double akk;
private Double sb;
private Double gk;
private Integer boni;

Daten(double gewicht, double preisProStueck, double gewinn, double EB, double AKK, double SB, double GK, int boni) {
    this.gewicht = gewicht;
    this.preisProStueck = preisProStueck;
    this.gewinn = gewinn;
    this.eb = EB;
    this.akk = AKK;
    this.sb = SB;
    this.gk = GK;
    this.boni = boni;
}

static List<Daten> readData() throws IOException {
    Gson gson = new GsonBuilder().create();
    List<Daten> allSaves = new ArrayList<>();
    if (new File(System.getProperty("user.home"), "/Wirtschaft.json").exists()) {
        JsonReader jReader = new JsonReader(new FileReader(new File(System.getProperty("user.home"), "/Wirtschaft.json")));

        BufferedReader br = new BufferedReader(new FileReader(new File(System.getProperty("user.home"), "/Wirtschaft.json")));
        if (br.readLine() != null) {
            allSaves = gson.fromJson(jReader, List.class);
        }
        br.close();
        jReader.close();
    }

    return allSaves;
}

private static void writeData(List<Daten> allEntries) throws IOException {
    PopUp pop = new PopUp();
    Gson gson = new GsonBuilder().create();
    JsonWriter writer = new JsonWriter(new FileWriter(new File(System.getProperty("user.home"), "/Wirtschaft.json")));
    gson.toJson(allEntries, List.class, writer);
    try {
        writer.flush();
        writer.close();
        pop.show("Zeugs wurde gespeichert!");
    } catch (IOException e) {
        pop.show("Trying to save data failed!");
    }
}

static void addData(Daten data) throws IOException {
    List<Daten> list = readData();
    list.add(data);
    writeData(list);
}

public Double getGewicht() {
    return gewicht;
}

public void setGewicht(Double gewicht) {
    this.gewicht = gewicht;
}

public Double getPreisProStueck() {
    return preisProStueck;
}

public void setPreisProStueck(Double preisProStueck) {
    this.preisProStueck = preisProStueck;
}

public Double getGewinn() {
    return gewinn;
}

public void setGewinn(Double gewinn) {
    this.gewinn = gewinn;
}

public Double getEb() {
    return eb;
}

public void setEb(Double eb) {
    this.eb = eb;
}

public Double getAkk() {
    return akk;
}

public void setAkk(Double akk) {
    this.akk = akk;
}

public Double getSb() {
    return sb;
}

public void setSb(Double sb) {
    this.sb = sb;
}

public Double getGk() {
    return gk;
}

public void setGk(Double gk) {
    this.gk = gk;
}

public Integer getBoni() {
    return boni;
}

public void setBoni(Integer boni) {
    this.boni = boni;
}}

共 (0) 个答案