Reputation: 23
I am using NetBeans IDE 8.2 for JavaFX. I already know that in order to change the position of a button I need to use setLayoutX/Y. I have tried this, and there is no effect on the buttons. Here is my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication2;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author coolDawg1234
*/
public class JavaFXApplication2 extends Application {
@Override
public void start(Stage primaryStage) {
String x = "1";
String y = "0";
Button btn1 = new Button(x);
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.print(x);
}
});
Button btn2 = new Button(y);
btn2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.print(y);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn1);
btn1.setLayoutX(250);
btn1.setLayoutY(220);
root.getChildren().add(btn2);
btn1.setLayoutX(200);
btn1.setLayoutY(200);
Scene scene = new Scene(root, 1000, 1000);
primaryStage.setTitle("WHAT\'s GOOD MY MANS");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Netbeans gives me 0 errors for this, and everything other than the position of the buttons looks fine to me.
Please help me find my problem.
Upvotes: 1
Views: 5578
Reputation: 1
30 root.getChildren().add(btn1);
31 btn1.setLayoutX(250);
32 btn1.setLayoutY(220);
33 root.getChildren().add(btn2);
34 btn1.setLayoutX(200);
35 btn1.setLayoutY(200);
Just take the right variable (consistent) btn1 gets two coordinates for the same direktion (250 for X in row 31, and 200 for X in row 34), so that button which has no directly set coordinates, has in this layout the coordinates (0,0).
Upvotes: 0
Reputation: 8363
The container for your buttons is a StackPane
. StackPane
is an implementation of Pane
such that it will, by default, layout its children at the center of itself. Therefore, whenever the scene needs to perform a layout, StackPane
will set the layoutX
and layoutY
values (therefore overwriting whatever you had set) in its layoutChildren()
method based on its own layout strategy. This behavior happens for most, if not all, subclasses of Pane
.
If you need to manually position your child nodes, you need to use the generic Pane
container. You can either choose to subclass it and provide your own layout strategy/logic, or simply set layoutX
and layoutY
values on the child nodes directly.
If you need the layout strategy provided by StackPane
, but you would want it to be positioned slightly different from the default position, then you may be looking for translateXProperty()
and translateYProperty()
.
Upvotes: 5