更新/刷新来自MongoDB数据库的JavaFX标签(JAVA)

huangapple 未分类评论48阅读模式
英文:

Update/Refresh JavaFX Label From MongoDB database (JAVA)

问题

我请求您的帮助,因为我无法更新来自我的数据库的JavaFX标签中的数据。
具体来说,我尝试创建了一个包含标签的MVC模式的视图。

我创建了模块以从MongoDB中提取所有数据(这些数据正在正常工作)。为了刷新我的视图,我在我的模型中创建了一个线程,每隔几秒钟从数据库中提取数据。
我收集的所有数据都存储在SimpleDoubleProperty/SimpleIntegerProperty/SimpleStringProperty中,以进行绑定。

我的模型类的一部分:

public SimpleDoubleProperty sto = new SimpleDoubleProperty(0.);
public SimpleIntegerProperty nbA = new SimpleIntegerProperty(0);
public SimpleStringProperty signalStrengh = new SimpleStringProperty();
public void start() {
    worker = new DataBaseWorker();
    worker.start();
}

private class DataBaseWorker extends Thread {
    public void run() {
        while(!stop) {
            sto.set(MongoDataService.getStorage());
            nbA.set(MongoDataService.getNbAircraft(interval));
            double sig_strengh = MongoDataService.getSignalStrengh(interval);
            if(sig_strengh >= excellent_sig) {
                signalStrengh.set("Excellent");
            }
            else if (sig_strengh > good_sig && sig_strengh < excellent_sig) {
                signalStrengh.set("Good");
            }
            else {
                signalStrengh.set("Poor");
            }
            waitFor(ConfigService.getInstance().getDBFetchDataIntervalInMS());
        }
    }   
}

此后,我的控制器进行了绑定(我还尝试在我的视图中进行绑定,但没有成功):

Label data_sto = (Label)((HBox)supervisor.root.getChildren().get(2)).getChildren().get(1);
Label nbAir_lab = (Label)((HBox)supervisor.root.getChildren().get(3)).getChildren().get(1);
Label sig_lab = (Label)((HBox)supervisor.root.getChildren().get(4)).getChildren().get(1);
data_sto.textProperty().bind(model.sto.asString());
nbAir_lab.textProperty().bind(model.nbA.asString());
sig_lab.textProperty().bind(model.signalStrengh);

一切都很好编译,没有运行时错误。我有我的窗口,其中包括我创建的一切。
但问题是,我的标签没有按照我期望的方式更新。

我认为问题可能来自我创建的线程,它不能与绑定器通信,但因为我是Java和JavaFX的新手,所以可能是任何原因。

谢谢,我希望有人能帮助我。

英文:

I'm asking your help because I can't manage to update javaFX Labels with data from my database.
To be specific, I tried to create a MVC pattern, with a view containing Labels.

I created modules in order to fetch all the data from MongoDB(which are workinf fine). In order to refresh my view, I created in my model a thread which fetch every few second data from the database.
All the data that I collect are stocked in SimpleDoubleProperty/SimpleIntegerProperty/SimpleStringProperty, in order to make biding.

A part of my model class :

public SimpleDoubleProperty sto = new SimpleDoubleProperty(0.);
public SimpleIntegerProperty nbA = new SimpleIntegerProperty(0);
public SimpleStringProperty signalStrengh = new SimpleStringProperty();
public void start() {
	worker = new DataBaseWorker();
	worker.start();
}

private class DataBaseWorker extends Thread {
	public void run() {
		while(!stop) {
			sto.set(MongoDataService.getStorage());
			nbA.set(MongoDataService.getNbAircraft(interval));
			double sig_strengh = MongoDataService.getSignalStrengh(interval);
			if(sig_strengh &gt;= excellent_sig) {
				signalStrengh.set(&quot;Excellent&quot;);
			}
			else if (sig_strengh&gt; good_sig &amp;&amp; sig_strengh &lt;excellent_sig) {
				signalStrengh.set(&quot;Good&quot;);
			}
			else {
				signalStrengh.set(&quot;Poor&quot;);
			}
			
			waitFor(ConfigService.getInstance().getDBFetchDataIntervalInMS());
			
		}
	}	
} 

Thereafter, my controller make the bindings (I also tried to make the bindings in my view without success) :

    Label data_sto = (Label)((HBox)supervisor.root.getChildren().get(2)).getChildren().get(1);
	Label nbAir_lab = (Label)((HBox)supervisor.root.getChildren().get(3)).getChildren().get(1);
	Label sig_lab = (Label)((HBox)supervisor.root.getChildren().get(4)).getChildren().get(1);
	data_sto.textProperty().bind(model.sto.asString());
	nbAir_lab.textProperty().bind(model.nbA.asString());
	sig_lab.textProperty().bind(model.signalStrengh);

Everything compile great and no runtime error. I have my window with everything I created.
But the issue is that my label are not updated as I expect.

I think the issue may come from the thread I create which doesn't communicate with the binders but because I'm a novice in Java and Javafx it could be anything.

Thank you and I hope that someone will help me.

huangapple
  • 本文由 发表于 2020年4月3日 20:20:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61011824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定