1

I want to set the text of a label from the login screen and when the other screen appears the text of the label shows.

For this I do the following:

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

Form Loader Class:

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

But after loading the main.fxml the label only shows "Label".

Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
smtaibur
  • 11
  • 1
  • 2
  • 1
    You should format your question. – Michaël Nov 28 '12 at 12:21
  • And consider trimming the code to the required minimum. – Henrik Aasted Sørensen Nov 28 '12 at 12:21
  • Maybe this can help: http://stackoverflow.com/questions/12166786/multiple-fxml-with-controllers-share-object I use this to pass values from one FXML file to another – Perneel Nov 29 '12 at 09:31
  • What's the exception? I also don't quite understand the relationships between the code... One thing that jumps out is that you're using a lookup before the node on which you're performing it has been displayed: lookups in general only work once the node has been rendered to the screen (so the css has been applied). – James_D Mar 19 '14 at 02:52
  • Maybe look at [this example](https://github.com/james-d/Shared-Data-Controller/tree/master/src) which shares data between two controllers, or [this](https://github.com/james-d/Dialog-FXML-Example/tree/master/src) which does the same in a slightly different way. Not sure if those are what you're looking for. – James_D Mar 19 '14 at 02:54

0 Answers0