MenuBar to top of the app - javafx

I'm quite new to JavaFX and I have two methods - one returning a grid layout and the other one returning a HBox with a MenuBar, but fot the life of me I can't make it, so it's not overlapping (I want the grid to be a few pixels lower). I have this code in my start method:
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup);
rootGroup.getChildren().add(addBar(stage.widthProperty()));
rootGroup.getChildren().add(addGridPane());
stage.setScene(scene);
stage.show();
How do I fix this?

You may use another Layout Manager as root. For example:
final VBox rootGroup = new Vbox();
Then all its children will be aligned vertically.

Related

JavaFX 3D boxes overlapping each other

I am having a problem with JavaFX 3D, the problem is as follows:
When I turn my perspective camera around, the last added box (blue box) overlaps the first added box (red box), here is a screenshot:
can anyone tell me why is this happening? And is there a way to fix it? (the boxes are literally 2 box classes with a width, height, depth, position and color)
Minimal reproducible example since somebody asked for it:
Box box1 = new Box();
Box box2 = new Box();
box1.setWidth(300);
box2.setWidth(300);
box1.setHeight(300);
box2.setHeight(300);
box1.setDepth(300);
box2.setDepth(300);
box1.setTranslateX(300);
box2.setTranslateX(300);
box1.setTranslateY(300);
box2.setTranslateX(300);
Group root = new Group();
root.getChildren().addAll(box, box2);
PerspectiveCamera cam = new PerspectiveCamera();
Scene scene = new Scene(root);
scene.setCamera(camera);
stage.setScene(scene);
stage.show();
where stage is the stage inside public void start(Stage stage), JavaFX's default run method (any class that extends Application should implement it)
You probably have to add a subscene with the depth buffer enabled. See: https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/SubScene.html#%3Cinit%3E(javafx.scene.Parent,double,double,boolean,javafx.scene.SceneAntialiasing)
Solution:
I used the following constructor:
new Scene(root, WIDTH, HEIGHT, true, SceneAntialiasing.BALANCED);
instead of:
new Scene(root);

JavaFx sizing layouts

I'am not familiar with JavaFx so I'am trying to code my JavaFx Components in Java Controller classes and calling them in main, my problem is that I cant size my layouts with all the width of the the window, I have this simple code :
public Acceuil(){
LAB_POST = new Button("Posts");
LAB_EVENT = new Button("Evennements");
.....
// my two boxes
VBox leftPane = new VBox();
FlowPane bpane = new FlowPane();
leftPane.setPadding(new Insets(20));
leftPane.setSpacing(30);
leftPane.setAlignment(Pos.CENTER);
leftPane.setStyle("-fx-background-color:#34495e");
leftPane.setMinWidth(300);
leftPane.setPrefSize(300, 600);
bpane.setStyle("-fx-background-color: red;"); // to see the space that it took
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
leftPane.getChildren().addAll(TXT_SEARCH, LAB_POST....);
this.getChildren().addAll(leftPane,bpane);
this.setMinHeight(600);
this.setMinWidth(900);
}
(Ps: I tried the FlowPane,BorderPane,AnchorPane and even Boxes)
So I'am Expecting to have the bpane in all the space that remains in my window but I get this,
So I'am wondering what I need to type to let the bpane take all the space, Thank you
I agree with Sedrick we need to know what is the container of these elements but you can try this:
bpane.setStyle("-fx-background-color: red;");
HBox.setHgrow(bpane, Priority.ALWAYS); // Add this line
Also if it is inside an AnchorPane you can try this:
AnchorPane.setTopAnchor(this, 0.0);
AnchorPane.setRightAnchor(this, 0.0);
AnchorPane.setBottomAnchor(this, 0.0);
AnchorPane.setLeftAnchor(this, 0.0);

Resizeable Gridpane or other container

Hi I'm trying to create a simple layout that looks like this using JavaFX.
I would like the user to be able to drag/resize the middle bar. I've tried to make this using a GridPane. But I can't figure out how to get the middle resized. Perhaps GridPane is not the way to go. Both panes will contain a lot of other program code later on. Thanks!
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stageRef.setMaxWidth(primaryScreenBounds.getWidth());
stageRef.setMaxHeight(primaryScreenBounds.getHeight());
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
gridPane.setPadding(new Insets(10));
Scene scene = new Scene(gridPane);
VBox vBoxMain = new VBox();
vBoxMain.setPrefWidth((primaryScreenBounds.getWidth()/5)*4);
vBoxMain.setPrefHeight(primaryScreenBounds.getHeight());
TextArea textArea = new TextArea();
textArea.setWrapText(true);
textArea.setPrefHeight(primaryScreenBounds.getHeight());
vBoxMain.getChildren().addAll(textArea);
vBoxMain.isResizable();
VBox vBoxSide = new VBox();
vBoxSide.setPrefWidth((primaryScreenBounds.getWidth()/5));
vBoxSide.setPrefHeight(primaryScreenBounds.getHeight());
vBoxSide.isResizable();
gridPane.add(vBoxSide, 1,1,1,1);
gridPane.add(vBoxMain, 2,1,4,1);
stageRef.setScene(scene);
stageRef.show();
You could use a SplitPane:
A control that has two or more sides, each separated by a divider,
which can be dragged by the user to give more space to one of the
sides, resulting in the other side shrinking by an equal amount.
Then you add two others containers to this pane allowing the user to change the position of the divider. You can also set minimum widths for each component within the pane or set the position of each divider within your code.

JavaFx Change position or Location of a button manually?

I have a problem with changing the location of my Button i want to set a Specific location where to put it but when i use .setLayoutX and .setLayoutY i even tried using .relocate and it is still not working am i doing something wrong? here is my sample code.
CODE
Button b1 = new Button();
b1.setText("Press");
b1.setLayoutX(100);
b1.setLayoutY(120); // this are just some randome numbers for this sample
StackPane pane = new StackPane();
pane.getChildren().add(b1);
Scene scene = new Scene(pane,500,500);
stage.setScenes(scene);
stage.show();
when i run the program the button is still in the center of the screen am doing something wrong?
Use Anchor pane or a pane to align nodes as per your layout.
StackPane, GridPane and many panes they come with inbuilt layout methods, you dont have control on locating the nodes based on x and y values.

JavaFX: Menu items only shown as three dots when in BorderPane

I am trying to learn JavaFX and I've run into a problem with the Menus in my MenuBar. Here is a minimal example:
public void start(Stage mainStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 1200, 1000, Color.WHITE);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("_File");
menuBar.getMenus().add(menuFile);
MenuItem add = new MenuItem("_New");
menuFile.getItems().add(add);
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(mainStage.widthProperty());
mainStage.setScene(scene);
mainStage.show();
}
This application starts, but the Menu in the MenuBar is only shown as three dots (...). It does open however when I press ALT+F, so it is there.
From what I understand, a Menu item has no width or similar attribute, so that can't be set. I suspect it has something to do with my root node being a BorderPane, because in every other example I found that works, the root is either a VBox or something else. I seem to get the desired results when I place a Vbox as my root node, and then add the MenuBar and the BorderPane` to the root - but that seems like a strange and uneccesary workaround to me.
So what am I missing here? Is it true that a MenuBar will only ever look like it should in certain containers? What is the difference between a BorderPane and a VBox in this regard? If someone could explain or point me to a part of the documentation that I've missed, I would be very thankful.
You are using a BorderPane and using getChildren().add() to add MenuBar in it, which is incorrect. BorderPane unlike VBox, can't accept any number of children and is divided into 5 specific positions :
top
left
right
bottom
center
The children goes into any one of these positions. Please go through the documentation for BorderPane.
You need to add the Menubar to the top of the BorderPane using :
root.setTop(menuBar);

Resources