JavaFX 应用启动时初始化更新UI

在JAVAFX启动时,有时想把应用开始的配置加载完毕,成功或者失败进行意识,一般写在start()方法中,由于Fxml模板绑定的是另一个controller,在Main的start方法在重新new或者静态方法等都无法获得UI线程,可以使用下面的方法:
先看一下效果:
在这里插入图片描述

启动类中的start方法

@Override
	public void start(Stage primaryStage) throws Exception {
		FXMLLoader fxmlLoader = new FXMLLoader();
		URL url = getClass().getResource("/html/index.fxml");
		fxmlLoader.setLocation(url);
		fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
		Parent root = fxmlLoader.load();
        primaryStage.setTitle("QN10_PRO_3K");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root));
        IndexController indexController = fxmlLoader.getController();
        indexController.init();
        primaryStage.show();  
	}

controller中更新UI

@FXML
private TextArea Text_Area_Info;//信息显示区域
public void init() {
		try {
			File file = new File(Main.configFileName); 
			if(!file.exists()) {
				this.Text_Area_Info.appendText("配置文件加载失败!");
			}else {
				Main.con.setConfigurationMap(new ParseXML().parseTypeXML(Main.chipName,Main.configFileName));
				ConfigurationFactory.chipElementMap.put(Main.chipName, Main.con);
				Device[] devices = Service.listDevices();
				if(devices != null && devices.length > 0) {
					AbstarctOperationFactory.device = devices[0];
					AbstarctOperationFactory.device.open();
					//System.out.println("已连接:"+AbstarctOperationFactory.device);
				}else {
					this.Text_Area_Info.appendText("没有可连接设备,5秒后退出!!!");
					new Thread(new Runnable() {
						@Override
						public void run() {
							try {
								java.util.concurrent.TimeUnit.SECONDS.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							System.exit(0);
						}
					}).start();
				}
			}
		} catch (Exception e) {
			
		}
		
		
		
	}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里藍天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值