Multithreading Javafx:使用线程自动更新表单元格
我有一个Multithreading Javafx:使用线程自动更新表单元格,multithreading,javafx,Multithreading,Javafx,我有一个Trade类,它包含一个属性currentPrice,它使用getPricedata()方法从网站下载价格数据。Trade对象将在TableView中显示为表行。现在,我的任务:是 使用getPricedata()方法从internet获取数据,在创建对象时填充currentPrice单元格 创建对象并更新表格单元格后,每隔1分钟重新启动getPricedata()方法 下面是我的代码的基本结构。但我不知道如何实现这一点? 我需要哪个包裹任务服务计划服务 public class T
Trade
类,它包含一个属性currentPrice
,它使用getPricedata()
方法从网站下载价格数据。Trade
对象将在TableView
中显示为表行。现在,我的任务:是
- 使用
方法从internet获取数据,在创建对象时填充getPricedata()
单元格李>currentPrice
- 创建对象并更新表格单元格后,每隔1分钟重新启动
方法李>getPricedata()
public class Trade{
private DoubleProperty currentPrice;
// need thread here
public double getPricedata(){
.......
}
}
使用
ScheduledService
,其Task
的call()
方法检索并返回值。然后,您可以向服务注册一个onSucceeded
处理程序,或者只将Trade
的currentPrice
绑定到service.lastValue()
。调用服务上的setPeriod(..)
(一次),将其配置为每分钟运行一次
由于正在从服务中设置currentPrice
,因此您应该只从Trade
类中公开ReadOnlyDoubleProperty
(否则您可以尝试调用currentPriceProperty().set(…)
或setCurrentPrice(…)
,这将在绑定时失败)
我会做类似的事情
public class Trade {
private final ReadOnlyDoubleWrapper currentPrice ;
private final ScheduledService<Number> priceService = new ScheduledService<Number>() {
@Override
public Task<Number> createTask() {
return new Task<Number>() {
@Override
public Number call() {
return getPriceData();
}
};
}
};
public Trade() {
priceService.setPeriod(Duration.minutes(1));
// in case of errors running service:
priceService.setOnFailed(e -> priceService.getException().printStackTrace());
currentPrice = new ReadOnlyDoubleWrapper(0);
currentPrice.bind(priceService.lastValueProperty());
startMonitoring();
}
public final void startMonitoring() {
priceService.restart();
}
public final void stopMonitoring() {
priceService.cancel();
}
public ReadOnlyDoubleProperty currentPriceProperty() {
return currentPrice.getReadOnlyProperty();
}
public final double getCurrentPrice() {
return currentPriceProperty().get();
}
private double getPriceData() {
// do actual retrieval work here...
}
}
公共级贸易{
私人最终只读双包装当前价格;
private final ScheduledService priceService=新ScheduledService(){
@凌驾
公共任务createTask(){
返回新任务(){
@凌驾
公用电话号码{
返回getPriceData();
}
};
}
};
公共贸易(){
priceService.setPeriod(持续时间:分钟(1));
//如果运行服务时出现错误:
priceService.setOnFailed(e->priceService.getException().printStackTrace());
currentPrice=新的ReadOnlyDoubleWrapper(0);
currentPrice.bind(priceService.lastValueProperty());
开始监视();
}
公共最终作废开始监控(){
priceService.restart();
}
公共最终失效监测(){
priceService.cancel();
}
public ReadOnlyDoubleProperty currentPriceProperty(){
返回currentPrice.getReadOnlyProperty();
}
公共最终双getCurrentPrice(){
返回currentPriceProperty().get();
}
私有双getPriceData(){
//在这里做实际的检索工作。。。
}
}
(在这里输入的代码没有经过测试,但它应该会告诉您这个想法。)HI@James\u D,您介意给我一个示例代码吗?这对我来说有点难理解。谢谢使用
DoubleProperty
是否是一个好的选择?请参阅更新(代码未经测试,无论是在编译级别还是在运行级别)。执行testTrade.getCurrentPrice()
时,我收到一条异常错误消息<代码>线程“main”java.lang.IllegalStateException中的异常:工具包未初始化;听起来好像您正在尝试在JavaFX应用程序之外运行此应用程序。对此表示抱歉。我不应该在JavaFX应用程序之外运行它。我创建了一个单独的类来检查我的getPriceData()
是否正常工作,它是否能够获得正确的数据,然后我在控制台上打印了出来。但是,当我将其绑定到JavaFX应用程序中时。该列仅显示当前价格的0。我不明白为什么会这样。尽管如此,我确信我的getPriceData()
代码运行良好。