0

I'm using this code to change the scene on my first screen.

Action button gotoScene2:

Node node=(Node) evento.getSource();
Stage stage=(Stage) node.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("MY_SCENE_2.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

So the code is working fine. Now I'm in the controller correspond to MY_SCENE_2 and I need to add a datepicker from code and not using SceneBuilder or something like that.

How can I add a datepicker(I mean it could be textfield or whatever) to my current scene (MY_SCENE_2)?

This is my code:

final DatePicker datePicker = new DatePicker(LocalDate.now());

datePicker.setOnAction(event -> {
    LocalDate date = datePicker.getValue();
    System.out.println("Selected date: " + date);
});

stage.setScene(
    new Scene(datePicker)
);
stage.show();

I need the stage value in order to succeed? How can I do that?

napstercake
  • 1,815
  • 6
  • 32
  • 57
  • You may find some of the techniques in this StackOverflow answer useful: [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). – jewelsea Apr 04 '14 at 19:23

1 Answers1

0

Just inject the pane into which you're going to add the DatePicker into the controller, and add the DatePicker to it.

@FXML
private VBox somePane ; // can be any kind of Pane, fx:id in fxml matches variable name
// ...

// in some handler method (or initialize):
final DatePicker datePicker = new DatePicker(LocalDate.now());
datePicker.setOnAction(...);
somePane.getChildren().add(datePicker); 
James_D
  • 201,275
  • 16
  • 291
  • 322