问题标签 [javafx-bindings]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - Why does binding a TextField to a property which is being updated on another thread end up with the application throwing errors?
I have a javafx application where I have multiple tabs (timer, stopwatch, clock) each with a separate Controller, and the user is able to add and independently start multiple timers using a start/stop button.
I tried binding a TextField to a property of another class MyTimer which keeps track of the elapsed time, but it eventually (after running for a couple of seconds) starts throwing an error. (If you check the code below, note that it only happens if the "Thread.sleep" is set to 10ms - when i increase the delay to 100ms, it kept running for about a minute and did not crash - I did not test further, since I would like to solve the root cause instead of increasing the delay).
Just so you have a quick idea: app image
MyTimer class:
While trying to implement the binding, I tried to bind the one default TextProperty which exists without any user interaction at application startup to the property representing the remaining time from the MyTimer class to the value in the Controller:
The Main method which starts it all up is fairly standard, but I'll include it anyway:
And finally, the stack trace: First, there's this:
And then the following errors keep repeating:
When I used Platform.runLater() in the MyTimer.start() function, it did work, like this:
But it seems wrong to use Platform.runLater like this, or is it fine? I understand that the GUI should not be updated outside of the FX thread, but I thought that binding properties takes care of this - that updating the property bound to a GUI element does not need to take place in the FX thread, and the GUI update would indeed be handled properly on the FX thread as part of the binding...
Ultimately, my solution which seems to work fairly well is to not use binding, but instead update the fields periodically in the Controller itself (the following method is called upon "onSelectionChanged" of the Tabs themselves) - but I was wondering how to make the binding work, as it seems that binding is the better practice. Anyway, here is the code which also does work:
My question then is, what is the correct approach to this problem, please?
javafx - 将 CheckBoxTableCell 绑定到 BooleanBinding
我想将 TableViewCell 中的 CheckBox 绑定到 BooleanBinding。下面的示例由一个带有列的 TableViewname
和isEffectiveRequired
. 列中的复选框绑定到表达式:
isRequired.or(name.isEqualTo("X"))
因此,当行中的项目是必需的或名称是 X 时,该项目是“有效必需的”,那么表达式应该为真。不幸的是,CheckBox 没有反映更改。为了调试,我添加了一个文本字段,显示nameProperty
和requiredProperty
计算的effectiveRequiredProperty
。
有趣的是,当只返回 isRequiredProperty 而不是绑定时,复选框起作用。
那么就 CheckBox 而言,Property 和 ObservableValue 有什么区别?
在名称中键入 X 时,应选中该行中的复选框。
在名称中键入 X 时,不会选中该行中的复选框。它从来没有被检查过,就像它根本没有被绑定一样。
java - JavaFX 中的计时器更新标签(绑定)
我想在我的应用程序中添加一个时钟,告诉您执行该任务的时间。为了简化它,我在一个新线程中包含了一个每秒递增的计数器,并用计数器编号更新标签“setTimer”。为此,我在 .fxml 文件中有一个标签 fx:id="setTimer" 并将其导入我的班级。
并在我的类中创建了另一个类,它扩展了线程 TimerTask 并在每次调用时将计数器加一。创建了一个新的对象“文本”,它应该始终使用计数器的当前值进行更新。
为了让这个类每秒调用一次,我在 initialize 方法中创建了一个计时器并将其设置为一秒。
目前我收到异常“不在 FX 应用程序线程上;currentThread = Timer-0'。
我尝试了很多方法来解决我的问题,但我还没有找到正确的点。我对自己想做的事情的想法应该很清楚,如果有人可以帮助我,我会很高兴。我的问题是更新 GUI 中计数器的更改。它不必像我想象的那样解决,只需要关于如何最好地实现它的提示。
谢谢
java - 如何在 JavaFX 图表中使用绑定?
我正在使用 JavaFX 和他们的 Charts 库编写一些“可视化 + 编辑数据”应用程序。我正在使用 MVC 模型,所以图表都准备好在 FXML 文件中初始化并fx:id
在 Controller 类中引用。
所以目前我实际上可以从文件中导入一些数据并通过这段代码将其可视化
但我需要能够使用表格或交互式折线图更改我的数据,因此它完全是双向绑定。目前我没有在图表中实现交互性,但我知道通过将EventListener
s 添加到图表幼稚节点(例如点或路径)的方法。
主要问题是如何在图表中绑定数据,而不是从其他对象添加数据?AFAIK 我什至可以设置在这种关系中计算数据的函数(在我的情况下需要软对数)。
我发现很少有关于绑定的文章,它们对我来说并不清楚。你能建议我一个带有注释的示例代码来了解如何在我的情况下使用它吗?而且我对哪些情况下数据将被某些分配自动解除绑定感兴趣(使用运算符=
)?如果它与分配解除绑定,那么使用它而不是通过引用传递有什么意义?
UPD 我找到了交互式曲线点的这种解决方案
在这种情况下,存储在我分配图表点的对象中的数据不会改变。