- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
正如标题所说。基本上我有一个 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"));
这一切都有效,尽管我更喜欢非工厂方法。但是我不知道如何与条形图进行相同类型的链接。
我的条形图有一个 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!"));
我尝试按照克里斯的建议执行此操作,但程序没有打印任何内容。 observableArrayList() 可能会返回一个新列表,而不是包装现有引用,但这意味着我的表也无法工作。可能出了什么问题?
编辑3:
错误在于,当引用的值发生变化时,可观察量会“变化”。可观察列表的值是其他引用,在我的例子中,这些引用被添加一次并且再也不会被触及。这意味着它在程序启动后的生命周期内实际上永远不会改变。我发现我可以将监听器附加到 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
,您可以添加 ListChangeListener
并在列表更改时调用图表更新函数。
ObservableList<DiceRoll> myList = FxCollections.observableArrayList();
myList.addListener((ListChangeListener)(c -> { updateMyChart() }));
然后在 updateMyChart()
中您将迭代列表:
private void updateMyChart() {
/* Clear series */
for(int i = 0; i < myList.size(); i++ {
/* Create new Series */
}
/* Add Series */
}
您只需将 DiceRoll
对象添加到列表中即可。
myList.add(new DiceRoll(1));
P.S - 我省略了创建/删除系列的步骤,因为您提到您已经可以做到这一点,如果您也需要查看,我可以将其添加进去。
关于java - 动态更新 JavaFX BarChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770775/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!