2015-12-17 37 views
0

我是JAVAFX的新手。我在JavaFx應用程序中有一個登錄表單。它具有在fxml文件中定義的標籤字段,其文本值在用不同消息進行認證後設置。請參閱下面的代碼。JAVAFX - 標籤字段未在動作事件處理程序上更新

CODE

if(userid>0){ 
    //the below setText call doesn't work and update the UI 
    actiontarget.setText("Modems are initializing. It may take longer time... "); 
    bootstrap =new Bootstrap(); 
    new Thread(){ 
     public void run(){ 
      bootstrap.run(); 
     } 
    }.start(); 
    while(!bootstrap.isSet()){ 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     // the below call also doesn't work 
     actiontarget.setText("Modems are getting initialized. Please wait for sometime.."); 
    } 
    serialModemList=bootstrap.getSerialModemList(); 
    getPanelList(serialModemList); 

}else{ 
    // this one method call works fine. not sure why? 
    actiontarget.setText("Invalid Terminal and Password "); 
} 

呼叫actiontarget.setText(..)當用戶有效不起作用。但是,如果用戶輸入無效的憑證,它將起作用。請讓我知道代碼有什麼問題。

FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.net.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.image.*?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 
    minHeight="-Infinity" minWidth="-Infinity" prefHeight="469.0" 
    prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"> 
    <center> 

     <GridPane fx:id="gridpane" xmlns:fx="http://javafx.com/fxml" 
      alignment="center" hgap="10" vgap="10"> 

      <Text text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0" 
       GridPane.columnSpan="2" /> 

      <Label text="Terminal" GridPane.columnIndex="0" 
       GridPane.rowIndex="1"> 
       <font> 
        <Font size="15.0" /> 
       </font> 
      </Label> 

      <TextField fx:id="txtuser" GridPane.columnIndex="1" 
       GridPane.rowIndex="1" /> 

      <Label text="Password" GridPane.columnIndex="0" 
       GridPane.rowIndex="2"> 
       <font> 
        <Font size="15.0" /> 
       </font> 
      </Label> 
      <PasswordField fx:id="txtaun" GridPane.columnIndex="1" 
       GridPane.rowIndex="2" /> 
      <HBox spacing="10" alignment="bottom_right" 
       GridPane.columnIndex="1" GridPane.rowIndex="4"> 
       <Button fx:id="loginbtn" onAction="#handleLoginAction" text="LOGIN" /> 
      </HBox> 
      <Label fx:id="message" GridPane.columnIndex="1" 
       GridPane.rowIndex="6" /> 
      <Label fx:id="actiontarget" GridPane.columnIndex="1" 
       GridPane.rowIndex="7" /> 
     </GridPane> 
    </center> 
    <top> 
     <Label id="Header" minWidth="400.0" prefHeight="81.0" prefWidth="174.0" 
      text="Sim Manager" BorderPane.alignment="CENTER"> 
      <font> 
       <Font size="50.0" /> 
      </font> 
     </Label> 
    </top> 
    <bottom> 
     <Label prefHeight="69.0" prefWidth="126.0" text="Powered by CIAR" 
      textFill="#20209e" BorderPane.alignment="bottom_right" /> 
    </bottom> 
</BorderPane> 

請讓我知道是否需要的任何信息。

+1

您在'while(!bootstrap.isSet()){...')中阻止了您的UI線程。對於後臺任務,請考慮使用['JavaFX Task'](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)。 –

回答