gpt4 book ai didi

JavaFX - TableView 不更新项目

转载 作者:行者123 更新时间:2023-12-01 08:48:03 24 4
gpt4 key购买 nike

我正在使用 JavaFX 为我的妻子编写一个小型词汇训练器。每个单词都存储它自己的统计值,例如她遇到该单词的频率以及她提供正确答案的频率。我已经为此设置了一个 TableView,但训练结束后统计数据不会更新。单词对象本身具有正确的值,但它们只是未显示在 TableView 中。通常,由于 ObservableList 的性质,TableView 总是显示对象的当前值,对吗?但在本例中并非如此。

这是我的词类:

public class Vocable
{
private String german;
private String english;
private int givenAnswers, correctAnswers, skippedAnswers;

public Vocable(String ger, String eng)
{
german = ger;
english = eng;
givenAnswers = 0;
correctAnswers = 0;
skippedAnswers = 0;
}

public String getGerman()
{
return german;
}

public String getEnglish()
{
return english;
}

public int getGivenAnswers()
{
return givenAnswers;
}

public void increaseGivenAnswers()
{
this.givenAnswers++;;
}

public int getCorrectAnswers()
{
return correctAnswers;
}

public void increaseCorrectAnswers()
{
this.correctAnswers++;
}

public int getSkippedAnswers()
{
return skippedAnswers;
}

public void increaseSkippedAnswers()
{
this.skippedAnswers++;
}
}

这是我的 View 类:

public class OverviewView extends VBox
{
private OverviewPresenter presenter;

private TableView<Vocable> overviewTable;
private TableColumn<Vocable, String> german;
private TableColumn<Vocable, String> english;
private TableColumn<Vocable, Number> given;
private TableColumn<Vocable, Number> correct;
private TableColumn<Vocable, Number> skipped;

private Button delete, back, showStats, add;

public OverviewView()
{
initView();
}

private void initView()
{
BorderPane pane = new BorderPane();

overviewTable = new TableView<>();
german = new TableColumn<>("German");
english = new TableColumn<>("English");
given = new TableColumn<>("given");
correct = new TableColumn<>("correct");
skipped = new TableColumn<>("skipped");

german.setResizable(true);
english.setResizable(true);
given.setResizable(true);
correct.setResizable(true);
skipped.setResizable(true);

delete = new Button("Delete");
delete.setOnAction(e -> presenter.deleteVocable(overviewTable.getSelectionModel().getSelectedItem()));

back = new Button("Back");
back.setOnAction(e -> presenter.showMainView());

showStats = new Button("Statistics");
showStats.setOnAction(e -> presenter.showStatistics());

add = new Button("Add Vocbale");
add.setOnAction(e -> presenter.showAddDialog());

overviewTable.getColumns().add(german);
overviewTable.getColumns().add(english);
overviewTable.getColumns().add(given);
overviewTable.getColumns().add(correct);
overviewTable.getColumns().add(skipped);

german.prefWidthProperty().bind(overviewTable.widthProperty().multiply(0.35));
english.prefWidthProperty().bind(overviewTable.widthProperty().multiply(0.35));
given.prefWidthProperty().bind(overviewTable.widthProperty().multiply(0.10));
correct.prefWidthProperty().bind(overviewTable.widthProperty().multiply(0.10));
skipped.prefWidthProperty().bind(overviewTable.widthProperty().multiply(0.10));

pane.setCenter(overviewTable);

HBox box = new HBox(50);
box.getChildren().addAll(back, delete, add, showStats);

pane.setBottom(box);
BorderPane.setMargin(box, Constants.DEFAULT_BP_MARGINS);

pane.setPadding(Constants.DEFAULT_PADDING);

getChildren().add(pane);
}

public void setTableContent(ObservableList<Vocable> list)
{
overviewTable.setItems(list);

german.setCellValueFactory(item -> new SimpleStringProperty(item.getValue().getGerman()));
english.setCellValueFactory(item -> new SimpleStringProperty(item.getValue().getEnglish()));

given.setCellValueFactory(item -> new SimpleIntegerProperty(item.getValue().getGivenAnswers()));
correct.setCellValueFactory(item -> new SimpleIntegerProperty(item.getValue().getCorrectAnswers()));
skipped.setCellValueFactory(item -> new SimpleIntegerProperty(item.getValue().getSkippedAnswers()));
}
}

知道如何解决这个问题吗?

最佳答案

使用JavaFX properties patternVocable中实现可变属性。这允许表格单元格观察适当的属性并在它们发生变化时做出响应。对于不变的值,例如 germanenglish,您仍然可以使用常规(和最终)字段。对于发生变化但您希望控制其更改方式的值(例如,您需要一个 increaseGivenAnswers() 方法,但没有通用的 setGivenAnswers(int) 方法),您可以使用ReadOnlyXXXWrapper 并公开 ReadOnlyXXXProperty。所以,例如:

public class Vocable
{

private final String german;
private final String english;
private final ReadOnlyIntegerWrapper givenAnswers = new ReadOnlyIntegerWrapper();
private final ReadOnlyIntegerWrapper correctAnswers = new ReadOnlyIntegerWrapper();
private final ReadOnlyIntegerWrapper skippedAnswers = new ReadOnlyIntegerWrapper();


public Vocable(String ger, String eng)
{
german = ger;
english = eng;
givenAnswers.set(0);
correctAnswers.set(0);
skippedAnswers.set(0);
}

public String getGerman()
{
return german;
}

public String getEnglish()
{
return english;
}

public ReadOnlyIntegerProperty givenAnswersProperty() {
return givenAnswers.getReadOnlyProperty() ;
}

public final int getGivenAnswers()
{
return givenAnswersProperty().get();
}

public void increaseGivenAnswers()
{
givenAnswers.set(getGivenAnswers()+1);
}


public ReadOnlyIntegerProperty correctAnswersProperty() {
return correctAnswers.getReadOnlyProperty();
}

public final int getCorrectAnswers()
{
return correctAnswersProperty().get();
}

public void increaseCorrectAnswers()
{
this.correctAnswers.set(getCorrectAnswers()+1);
}

public ReadOnlyIntegerProperty skippedAnswersProperty() {
return skippedAnswers.getReadOnlyProperty();
}

public final int getSkippedAnswers()
{
return skippedAnswersProperty().get();
}

public void increaseSkippedAnswers()
{
this.skippedAnswers.set(getSkippedAnswers()+1);
}

}

为了让单元格观察这些属性,请将单元格值工厂映射到已定义的属性:

german.setCellValueFactory(item -> new SimpleStringProperty(item.getValue().getGerman()));
english.setCellValueFactory(item -> new SimpleStringProperty(item.getValue().getEnglish()));

given.setCellValueFactory(item -> item.getValue().givenAnswersProperty());
correct.setCellValueFactory(item -> item.getValue().correctAnswersProperty());
skipped.setCellValueFactory(item -> item.getValue().skippedAnswersProperty());

关于JavaFX - TableView 不更新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com