Javafx 在JFX中动态更新边框窗格?

Javafx 在JFX中动态更新边框窗格?,javafx,Javafx,我在javafx场景中添加了一个包含文本的边框窗格 如何在javafx中更新场景 我希望这样做,以便我可以动态地反映更改 因此,样本如下所示: 我的gui:文本 动态更新 我的gui:更新的文本 public class JFXPnlRef extends JFXPanel { public JFXPnlRef(){ this.init(); } private void init(){ Platform.setImplicitExit(false);

我在javafx场景中添加了一个包含文本的边框窗格

如何在javafx中更新场景

我希望这样做,以便我可以动态地反映更改

因此,样本如下所示:

我的gui:文本

动态更新

我的gui:更新的文本

public class JFXPnlRef extends JFXPanel {
   public JFXPnlRef(){
      this.init();
   }
   private void init(){
       Platform.setImplicitExit(false);
       /* run gui main scene */
       Platform.runLater(new Runnable(){
           @Override
           public void run(){
               createScene();
           }  
       });
    }
    private void createScene(){
       /* we are in the JFX thread */
       /* set our look and feel style for the gui */
       borderPane = new BorderPane();
       final Text text = new Text();
       text.setFont(new Font(25));
       text.setText("javafx threads!");
       borderPane.setLeft(text);
       scene = new Scene(borderPane);
       this.setScene(scene);
    }
    public void myThread(){
        JFXPnlThrd.run( new Runnable() {
        @Override
        public void run() {
            final Text text = new Text();
            text.setFont(new Font(25));
            text.setText("inside the thread!");

            scene.getRoot().getChildrenUnmodifiable();
            Parent root = scene.getRoot();
            ObservableList<Node> i = root.getChildrenUnmodifiable();

            /* use the root node to add stuff to the border pane ? */

            revalidate();
            repaint();
            updateUI();
           }
       }); //end of thread
     }//end of method
}//end of class



/ * outter class to check  if we are in the javafx thread */
public class JFXPnlThrd {

     public static void run(Runnable task) {
       if(task == null) 
           throw new IllegalArgumentException(
                   "The task can not be null");
       if(Platform.isFxApplicationThread()) task.run();
       else Platform.runLater(task);
     }
}
公共类JFXPnlRef扩展JFXPanel{
公共JFXPnlRef(){
this.init();
}
私有void init(){
Platform.setImplicitExit(false);
/*运行gui主场景*/
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
createScene();
}  
});
}
私有void createScene(){
/*我们在JFX线程中*/
/*为gui设置我们的外观样式*/
borderPane=新的borderPane();
最终文本文本=新文本();
text.setFont(新字体(25));
setText(“javafx线程!”);
borderPane.setLeft(文本);
场景=新场景(边框窗格);
这是一个场景;
}
公开阅读{
JFXPnlThrd.run(新的Runnable(){
@凌驾
公开募捐{
最终文本文本=新文本();
text.setFont(新字体(25));
setText(“线程内!”);
scene.getRoot().getChildrenUnmodifiable();
父根=scene.getRoot();
ObservableList i=root.getChildrenUnmodifiable();
/*是否使用根节点将内容添加到边框窗格*/
重新验证();
重新油漆();
updateUI();
}
});//线程结束
}//方法结束
}//下课
/*outter类检查我们是否在javafx线程中*/
公共类JFXPnlThrd{
公共静态无效运行(可运行任务){
如果(任务==null)
抛出新的IllegalArgumentException(
“任务不能为空”);
if(Platform.isFxApplicationThread())task.run();
else Platform.runLater(任务);
}
}

谢谢ItachiUchiha

答案很简单

移动定义为的本地
文本

final Text text = new Text();
到类实例变量

private Text text;

谢谢ItachiUchiha

答案很简单

移动定义为的本地
文本

final Text text = new Text();
到类实例变量

private Text text;

场景元素由JavaFX自动更新。你不必明确地为此做任何事情。只需更改
文本/标签的文本
,它就会反映出。只需确保所有更改都在JavaFX应用程序线程上执行。感谢您对此进行调查并给出答复。我知道最好不要在其他地方修改场景元素,而是修改JFX应用程序。线然而,我不知道如何才能做到这一点。我将发布一个简单的代码示例,这样也许我可以在这里获得进一步的指导。谢谢。示例代码有帮助,但A总是更好;)我刚注意到你的MCVE建议。当然,在尽可能快地发布我的示例代码后,我会注意到:(.可怜的我,上网太慢了!好吧,我可以看看MCVE。我不会说谎,我还是习惯了。你的代码对我来说似乎没问题。每次你想对场景图做一些更改,你都可以通过调用
Platform.runLater()来确保它在JFX应用程序线程上运行。)
。代码是否按预期运行?JavaFX会自动更新场景元素。您不必为其显式执行任何操作。只需更改
文本/标签的文本即可反映出来。只需确保所有更改都在JavaFX应用程序线程上执行。感谢您的关注和回复。我知道最好不要在其他地方修改场景元素,而是修改JFX App.thread。但是,我不知道如何才能做到这一点。我将发布一个简单的代码示例,以便在这里获得进一步的指导。谢谢。示例代码有帮助,但a总是更好;)我刚刚注意到您的MCVE建议。当然,我会在尽可能快地发布我的示例代码后注意到:(.可怜的我,上网太慢了!好吧,我可以看看MCVE。我不会撒谎,我还是习惯了。你的代码对我来说似乎没问题。每次你想对场景图进行一些更改时,你都可以通过调用
Platform.runLater()
,确保它在JFX应用程序线程上运行。代码是否按预期运行?