Java:如何在其他控制器中设置JavaFX中标签的值

时间:2012-11-28 11:58:25

标签: java javafx-2

我想从登录屏幕设置标签的文本,当出现另一个屏幕时,标签的文字会显示。

为此我做了以下事情:

的LoginController:

public class LoginController {    

    @FXML
    private Label label;
    @FXML
    private TextField login_txtf_user_id,login_password_field;
    @FXML
    public Database_Connection dbc1;
    @FXML
    public String userid;

    @FXML
    private void LoginButtonAction(ActionEvent event) throws Exception {
        dbc1=new Database_Connection();
        dbc1.query();

        if(dbc1.login().equals(login_txtf_user_id.getText().trim())){  
            userid=login_txtf_user_id.getText();                 
            Parent root1=FXMLLoader.load(getClass().getResource("Main.fxml"));
            Label lblData= (Label) root1.lookup("#lblData");
            lblData.setText(userid);
            FormLoader fml=new FormLoader("Main.fxml");
            fml.show(event);                 
        }           
    }       
}

表单加载程序类:

public class FormLoader {

    String url;

    public FormLoader(String fxmlFile){
        this.url=fxmlFile;
    }

    public void show(ActionEvent event) throws Exception{
        Node node=(Node) event.getSource();
        Stage stage=(Stage) node.getScene().getWindow();
        Parent root = FXMLLoader.load(getClass().getResource(url));/* Exception */
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}

MainController:

public class MainController {

    @FXML
    private Label lblData,lbl_custo_name, lbl_bill_no, lbl_amount, lbl_txn_id, lbl_date, lbl_hyperlink, login_lbl_info;
    @FXML
    private Hyperlink hyper_payment_confirmation, hyper_cancel_payment_confirmation, hyper_valid_bill;
    @FXML
    public TextField login_txtf_user_id,txtf_custo_name, txtf_bill_no, txtf_amount, txtf_txn_id, txtf_date;
    @FXML
    private Button login_btn_login,btn_save;
    @FXML
    public Database_Connection wdb,wdb1;
    public String abc;

    public void usr_id(String usr){
        abc=usr;
    }

    @FXML
    protected void saveButtonAction(ActionEvent event) throws Exception {
    //      LoginController root=new LoginController();           
      wdb.query("Insert into WASA.BILL_DETAILS values ('"+txtf_bill_no.getText()+"','"+txtf_txn_id.getText()+"','"+txtf_custo_name.getText()+"',"+txtf_amount.getText()+")");
    //      wdb.query("Insert into WASA.TXN_DETAILS values ('"+txtf_txn_id.getText()+"','"+usr+"',6,'Khulna','"+txtf_date.getText()+"')");
      txtf_bill_no.setText(null);
      txtf_txn_id.setText(null);
      txtf_custo_name.setText(null);
      txtf_amount.setText(null);
      txtf_date.setText(null);               
    }

    @FXML
    private void hyper_payment_confirmation(ActionEvent event) throws Exception{
        lbl_hyperlink.setText("Payment Confirmation");
        lbl_hyperlink.setVisible(true);
        lbl_custo_name.setVisible(true);
        lbl_bill_no.setVisible(true);
        lbl_amount.setVisible(true);
        lbl_txn_id.setVisible(true);
        lbl_date.setVisible(true);

        txtf_date.setVisible(true);
        txtf_custo_name.setVisible(true);
        txtf_bill_no.setVisible(true);
        txtf_amount.setVisible(true);
        txtf_txn_id.setVisible(true);

        lblData.setVisible(true);    
    }
}

但加载main.fxml后,标签只显示“标签”

0 个答案:

没有答案