0

I have FXMLDocumentController.java and i wanna to send name(textfill) to anoterclass

public class FXMLDocumentController implements Initializable { 
@FXML
private Label label;
@FXML
private Button button;
@FXML
private TextField name;
private String sss = "";
@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
    sss=name.getText();
    setSss(sss);
    System.out.print(sss);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    
public String getSss() {
    return sss;
}
public void setSss(String sss) {
    this.sss = sss;
}   

and this another class login.java

public class Login extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
    FXMLDocumentController fx = new FXMLDocumentController();
    System.out.print(fx.getSss());
}   

i debug and fx.getSss is null what should i do? thank you for comment

  • See https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class on how to get the Controller. – Ralf Renz Jul 30 '18 at 12:57
  • Also note that `Application.launch` (usually) completes after the last window is closed. "Returning" something from the application is most likely not the best option. (The `Application` class should be used as entry point of the application...) – fabian Jul 30 '18 at 13:02
  • thank you @RalfRenz –  Jul 30 '18 at 13:08

2 Answers2

0

Never instantiate a controller in your code. Instead: FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); FXMLDocumentController fx = loader.getController();

Reda Meskali
  • 275
  • 3
  • 9
-1

Build getters and setters into the class design.

http://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices

Jonah Mann
  • 23
  • 5
  • There are getters/setters in the `FXMLDocumentController` class. Also how should this help? There is no instance of `Login` that could be refered to in the `static main` method... – fabian Jul 30 '18 at 13:01
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Jul 31 '18 at 08:43