MohOuss
MohOuss

Reputation: 81

How to position controls binding there position into the stage's size using the controller class in JavaFx?

I found some thing similar in this link How to call functions on the stage in JavaFX's controller file

and here is what I found in one of the answers

StageTrackingSample.java

public class StageTrackingSample extends Application {

  @Override public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(
  getClass().getResource(
    "stagetracking.fxml"
  )
);

final Parent root = (Parent) loader.load();
final StageTrackingController controller = loader.getController();
controller.initData(stage);
stage.setScene(new Scene(root));
stage.show();
  }

  public static void main(String[] args) { launch(args); }
} 

StageTrackingController.java

public class StageTrackingController {

@FXML private Label stageX;
public void initialize() {}  
public void initData(final Stage stage) {
 stageX.textProperty().bind(
  Bindings.format(
    "(%1$.2f, %2$.2f)", 
    stage.xProperty(), 
    stage.yProperty()
    )
   );
 }
}

I wanted to position the progressIndicator in the middle of the window, so I tried this in my controller class Controller.java

 public void initInterface(Stage stage) {

       progressIndicator.layoutXProperty().bind(stage.widthProperty().divide(2));
       progressIndicator.layoutYProperty().bind(stage.heightProperty().divide(2));
 }

and this in Main.java

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    final FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    final Parent root = loader.load();
    final Controller controller = loader.getController();
    final Scene scene = new Scene(root);
    controller.initInterface(primaryStage);
    primaryStage.setScene(scene);
    primaryStage.show();

 }


public static void main(String[] args) {
    launch(args);
 }
}

it doesn't work even when I tried passing the scene or the anchorpane(which is defined in the fxml file) as a parameter into initInterface method, it seems that it has problem with binding progressIndicator properties

Upvotes: 0

Views: 1347

Answers (1)

Brian Stallter
Brian Stallter

Reputation: 159

by using the layoutXProperty and layoutYProperty and binding them to the Stage's width and height you must be trying to put it in the lower right hand corner of the stage. You can achieve this much easier, and JavaFX insists you do so, by using layouts in your scene and making the scene fill the entire area in question.

"From the Javadocs for the layoutX property: If the node is managed and has a Region as its parent, then the layout region will set layoutX according to its own layout policy. If the node is unmanaged or parented by a Group, then the application may set layoutX directly to position it. What this means is that the LayoutX/Y properties are controlled by the parent (and so it should be able to 'set' them). However, when you bind them they cannot be set anymore resulting in " A bound value cannot be set" exception."

Here is a good tutorial on regions and how to get things to layout in SceneBuilder as you please. If you're not using SceneBuilder I recommend it. https://www.youtube.com/watch?v=zvgWgpGZVKc&list=PL6gx4Cwl9DGBzfXLWLSYVy8EbTdpGbUIG&index=35

Upvotes: 0

Related Questions