very new to JavaFX I'm following a simple tutorial here I created a new JavaFX project but it has a BorderPane as a default rather than a StackPane as the tutorial says, so I left it there. The application only has a button on it and if I use the BorderPane the button isn't displayed. If I change it to StackPane the button shows up. Thinking that for some reason the BorderPane was clipping something off, I made the application windows full size, but I still couldn't see the button. Here is the code with the BorderPane the one that doesn't display the button:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("This is a test!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Any idea?
Take a look at the docs about
BorderPane
:Therefore you need to use stuff like:
In your case
root.getChildren().add(btn);
should be for exampleroot.setCenter(btn);
.