【发布时间】:2017-06-27 00:52:00
【问题描述】:
正如标题所说。基本上我有一个 ArrayList ,其中 DiceRoll 是一个在 TableView 中成功使用的类,用于在更改值时更新值。
public class DiceRoll {
private final SimpleIntegerProperty rollNumber = new SimpleIntegerProperty();
private final SimpleIntegerProperty cubeRoll = new SimpleIntegerProperty(0);
private final SimpleIntegerProperty icosahedron = new SimpleIntegerProperty(0);
private final SimpleIntegerProperty dodecahedron = new SimpleIntegerProperty(0);
public DiceRoll(int rollNumber) {
this.rollNumber.set(rollNumber);
}
public void incrementRoll(DiceTypes type){
switch(type){
case CUBE:
this.cubeRoll.set(this.cubeRoll.getValue() + 1);
break;
case DODECAHEDRON:
this.dodecahedron.set(this.dodecahedron.getValue() + 1);
break;
case ICOSAHEDRON:
this.icosahedron.set(this.icosahedron.getValue() + 1);
break;
}
}
public int getRollNumber() {
return rollNumber.get();
}
public SimpleIntegerProperty rollNumberProperty() {
return rollNumber;
}
public int getCubeRoll() {
return cubeRoll.get();
}
public SimpleIntegerProperty cubeRollProperty() {
return cubeRoll;
}
public int getIcosahedron() {
return icosahedron.get();
}
public SimpleIntegerProperty icosahedronProperty() {
return icosahedron;
}
public int getDodecahedron() {
return dodecahedron.get();
}
public SimpleIntegerProperty dodecahedronProperty() {
return dodecahedron;
}
public void reset(){
cubeRoll.set(0);
icosahedron.set(0);
dodecahedron.set(0);
}
}
像这样:
rollNumberColumn.setCellValueFactory(new PropertyValueFactory<>("rollNumber"));
这一切都有效,尽管我更喜欢非工厂方法。但是我不知道如何使用 BarChart 进行相同类型的链接。
我的 BarChart 有一个 CategoryAxis 作为 X 轴,一个 NumberAxis 作为 Y 轴。我可以使用 XYGraph.Series 和 XYGraph.Data 方法成功添加数据,但这些方法不会自动更新。我该怎么做?
编辑:
TableColumn<DiceRoll, Number> rollNumberColumn = new TableColumn<>("Roll Number");
rollNumberColumn.setCellValueFactory(new PropertyValueFactory<>("rollNumber"));
tableView.setItems(FXCollections.observableArrayList(model.getDiceRolls())); //model.getDiceRolls() returns an ArrayList<DiceRoll>
tableView.getColumns().setAll(rollNumberColumn, cubeRollNumberColumn, dodecahedronRollNumberColumn, icosahedronRollNumberColumn);
DiceRoll 基本上被初始化为一个可以滚动的数字。我已经在我的表 x20 中为所有 20 个可能的滚动添加了这个,如果它滚动就添加到它。在图表形式中,这意味着 3 个图表,X 轴表示滚动次数,Y 轴表示滚动次数。
工厂评论与我的问题无关,但我不太喜欢使用这里使用的工厂模式。
编辑2:
ObservableList<DiceRoll> testing = FXCollections.observableArrayList(model.getDiceRolls());
testing.addListener((ListChangeListener)c -> System.out.println("I'm working!"));
我尝试按照 Chris 的建议执行此操作,但程序不打印任何内容。可能是 observableArrayList() 返回一个新列表而不是包装现有引用,但这意味着我的表也不起作用。有什么问题?
编辑3:
错误在于当引用的值发生变化时,可观察对象“变化”。并且 observablelist 的值是其他引用,在我的例子中,它们被添加一次并且再也不会被触及。这意味着它在启动后程序的生命周期中实际上永远不会改变。我发现我可以将侦听器附加到 DiceRoll 中的 IntegerProperties 以在其中一个发生变化时做任何我想做的事情,这很棒并且给了我非常需要的控制权。
编辑4
this.model.getDiceRolls().parallelStream().forEach(diceRoll -> {
diceRoll.cubeRollProperty().addListener((observable, oldValue, newValue) ->
cubeSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
);
diceRoll.dodecahedronRollProperty().addListener((observable, oldValue, newValue) ->
dodecahedronSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
);
diceRoll.icosahedronRollProperty().addListener((observable, oldValue, newValue) ->
icosahedronSeries.getData().get(diceRoll.getRollNumber() - 1).setYValue(newValue)
);
});
这是我最后用来监控变化的。它所需要的只是你的类使用一个所谓的 *Property 变量,它支持开箱即用的侦听器。例如,当它发生变化时,您可以运行一段代码来更新图表。
【问题讨论】:
-
你能更清楚地解释一下你的数据结构和你的图之间的关系吗?您大概有一个
ObservableList<DiceRoll>作为表的项目(因此您有多个DiceRoll实例)。那么你想在图表中看到什么?类别轴上有什么,y 轴的对应值是什么? -
另外,“我更喜欢非工厂方法”是什么意思?
-
@James_D 更新了一些相关信息
标签: java arraylist javafx bar-chart fxml