JavaFX Controls in Default Window

Adding a Pane to the root StackPane will allow setting of static control position with setLayoutX()/setLayoutY(). Therefore, it will not be necessary to change the root pane in the runtime as proposed above. Presumably any of the other layout panes could be added to the pre-existing root pane, although this has yet to be tested.

// Adding a Pane to root StackPane will allow user to
// set control position with setLayoutX()/setLayoutY()

import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;

Canvas canvas;
StackPane root;
Pane pane;

void setup(){
  size(350,200,FX2D);
  surface.setTitle("JavaFX control in Default Wnd");
  canvas = (Canvas)surface.getNative();
  println("canvas =",canvas);
  root = (StackPane)canvas.getParent();
  println("root =",root);
  pane = new Pane();
  root.getChildren().add(pane);
  Button btn = new Button("Push Me");
  btn.setLayoutX(60);
  btn.setLayoutY(30);
  pane.getChildren().add(btn);
  println("pane =",pane);
}

Output:

1 Like