简体   繁体   English

JAVAFX-标签字段未在动作事件处理程序上更新

[英]JAVAFX - Label field not updating on action event handler

I'm a newbie to JAVAFX. 我是JAVAFX的新手。 I have a login form in JavaFx application. 我在JavaFx应用程序中有一个登录表单。 It has a Label field defined in fxml file, whose text value is set after authentication with different messages. 它具有在fxml文件中定义的Label字段,其文本值是在使用不同消息进行身份验证之后设置的。 Please see code below. 请参见下面的代码。

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 ");
}

The call actiontarget.setText(..) doesn't work when the user is valid. 用户有效时,调用actiontarget.setText(..)无效。 but, it works if the user enters invalid credentials. 但是,如果用户输入无效的凭据,它就可以工作。 Please let me know what's wrong with the code. 请让我知道代码有什么问题。

FXML XML文件

<?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>

Please let me know if any information is needed. 请让我知道是否需要任何信息。

I believe your problem is in using Thread.sleep you are halting the UI thread, which makes it impossible for it to update the display. 我相信您的问题是使用Thread.sleep暂停了UI线程,这使其无法更新显示。

Try using Task ( https://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm ) and set the text in the onSucceeded and onFailed . 尝试使用Taskhttps://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm )并在onSucceededonFailed设置文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM