简体   繁体   中英

JavaFx Set Label Text on Scene Open

I have an FXML file with an empty Label named welcomeText . It's the main Scene of my program and I would like to set the label text to something like that: Hello "username" when I start the program on Windows or Linux.

public class MainAdminController implements Initializable {

@FXML
private Label welcomeText;
final String username = System.getProperty("user.name");

@FXML
private void SetWelcome() {
welcomeText.setText("Hello " +username);
 }
}

But it isn't show anything. Any idea how can I set the Label properly when I open the scene? Thanks.

The SetWelcome method is superfluous.

Define an initialize() method for your controller and it will automatically be invoked when the FXMLLoader loads a new document linked to the controller.

public void initialize() {
    welcomeText.setText("Hello " +username);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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