)类 新建javafx程序时_JavaFX:在实例化控制器类时传递参数

本文讨论了在JavaFX应用中如何在加载FXML文件之前实例化控制器类并传递参数。通过两种方法实现:一是不指定fx:controller,在创建控制器实例时传入数据;二是指定fx:controller,然后从FXMLLoader获取控制器实例并设置数据。
摘要由CSDN通过智能技术生成

I am working on JavaFX application right now. All my gui is in .fxml format and through controller class manages all GUI components. However, I have difficulties with instantiating the controller class before I load FXML loader. I was unable to find a good solution for my question from others on stackoverflow, therefore this is not a duplicate question.

The reason why I am instantiating the controller class is that I want to pass some parameters so that these parameters will be displayed in GUI.

I am loading an FXML file the following way:

/*

* for Work Order button

*/

@FXML

private void pressWorkOrder() throws Exception{

WorkOrderController wo = new WorkOrderController("ashdkjhsahd"); //instantiating constructor

Parent parent = FXMLLoader.load(getClass().getResource("/fxml/WorkOrder.fxml"));

Scene scene = new Scene(parent);

Stage stage = new Stage();

stage.setScene(scene);

stage.setTitle("Word Order");

stage.setResizable(false);

stage.show();

}

And here is my actual Controller class:

public class WorkOrderController implements Initializable{

@FXML

private Button summary;

private String m,n;

public WorkOrderController(String str) {

// TODO Auto-generated constructor stub

m = str;

}

//for testing

public void set(String str){

m = str;

}

@FXML

public void check(){

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值