1

i am new to javafx and i want to transfer variable values from one controller to another and i have no idea how to do this. so please help me.

for example:

i want to display username from first login window to second dashboard window so what should i do to save userid in one variable and send it to second window and display there in a label.

code test:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
 * FXML Controller class
 *
 * @author wabcon
 */
public class AdmissionController implements Initializable {

int userid=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    userid=10001;

    }    

}

how could i send this userid to next window controller. please help me. Thank you.

user3204934
  • 485
  • 6
  • 15
  • i am new in javafx it to complicated can you explain in a simple way – user3204934 Jun 12 '14 at 18:06
  • You will need to understand the code in the answer to the existing question if you want to build the kind of application you are describing. – James_D Jun 12 '14 at 18:11
  • but i am unable to understand in that answer is `customer` is not set with any data how can i set data in `customer` – user3204934 Jun 12 '14 at 18:15

1 Answers1

1

I am assuming you are doing this for a custom component.

So, you create a class for your custom component and set that class as the controller:

public class CustomControl extends AnchorPane implements Initializable {
    String customId;

    public CustomControl() {
        //if you want to set a FXML
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/res/customControl.fxml"));
        //Defines this class as the controller
        fxmlLoader.setRoot(this);
        //this.getStylesheets().add("/res/style.css"); <- if you want to set a css
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }
        public String getCustomId() {
            return customId;
        }
    public void setCustomId(String customId) {
        return this.customId = customId;
    }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
          //Initializes the controller
    }
}

On your MainController:

CustomControl c = new CustomControl();
c.setCustomId("StackOverflow");
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Mansueli
  • 6,223
  • 8
  • 33
  • 57