2014-01-12 49 views
0

我需要从javafx中的句柄函数更新某些组件(标签,ProgressBar,按钮)。更新JavaFX中的组件GUI图形用户界面

该对话框是一个模式对话框,它用于按顺序执行一些操作。

@FXML public void updateHandle(ActionEvent action){ 

    buttonSend.setDisable(true); 

    /* Operation 1 */ 
    progressBar.setProgress(0.05); 
    label.setText("Init.."); 

    myInitFunction(); 
    myVar = new var(); //global 


    /* Op2 */ 
    progressBar.setProgress(0.10); 
    label.setText("Check connection.."); 

    myConnFunction(); 

    // .... 
    // .... 
} 

问题是我的所有功能都正确处理,但GUI上的元素没有改变。

编辑

我试图用Platform.runlater,但它似乎不工作...

void updateLabelLater(final Label label, final String text) { 
     Platform.runLater(new Runnable() { 
      @Override public void run() { 
      label.setGraphic(null); 
      label.setText(text); 
      } 
     }); 
    } 
void updateProgressBar(final ProgressBar progressBar, final double val){ 
    Platform.runLater(new Runnable() { 
      @Override public void run() { 
      progressBar.setProgress(val); 
      } 
     }); 
} 

回答

1

是updateHandle其上运行的事件线程?由于工具问题,我没有打扰FXML,所以这不是一个很好的答案。 (但希望它有帮助!)

//Pseudo Code 
public doLongRuningOperation(final Object ... objects){ 
    final Thread longRunning = new Thread(){ 
    public void run(){ 
     update("this"); 
     sleep(1000); //Pause/do something etc... 
     update("should"); 
     sleep(1000); 
     update("update"); 
     System.err.println("completed"); 
    } 
    }; 
    longRunning.start(); //Start this process 
} 
//Rejoin the UI Thread and update it... 
public void update(final String text){ 
    //reference to label 'lbl' 
    Platform.runLater(new Runnable(){ 
    lbl.setText(text); 
    }); 
} 
相关问题