How can I use HBox in JavaFX, to place certain elements at a specific location on the canvas?

701 views Asked by At

A newbie! I know this is the easiest thing to do in my whole project, but for some stupid(I'm the stupid) reason I'm stuck here. I want to use HBox to place a button at a specific (x,y) coordinate. Please help:

    //buttons
        //HBox hbox = new HBox(200);
        Button button = new Button(" Add to meal ");
    
    
        //get info on all customs
    
    
        Button button1 = new Button( "   Add to list   ");
    
        //hbox.getChildren().addAll(button);
    

//layout
        //add to meal button
        button.setLayoutX(125.0d);
        button.setLayoutY(325.0d);
    
        //add to list button
        button1.setLayoutX(130.0d);
        button1.setLayoutY(520.0d);
1

There are 1 answers

0
jewelsea On
Don’t use a HBox for absolute positioning

HBox isn`t the right layout for absolute positioning, it is an automated layout manager which sizes and positions stuff automatically based on an internal algorithm.

So, even though you are manually setting layout coordinates on child nodes in the HBox, the layout logic inside HBox will overwrite them with the values it calculates.

Instead use a layout that does absolute positioning

There are other layouts like AnchorPane, Pane, and Group that work with absolute positioning. You should research them to ascertain what would be best for your situation.

The simplest layout, which also allows resizing is a Pane, so, if you wanted, you could substitute your HBox for that.

Alternatives to absolute positioning

For most apps, the use of layout panes that provide greater automated layout features, such as Hboxes, VBoxes, BorderPanes, and GridPanes will usually be better choices than a plain Pane layout. These are usually used in conjunction with various configurations and layout hints for the panes, such as instructions on the default alignment of nodes within the pane, or the min/max/pref size of child nodes.


What follows isn't specific to the question, but just advice on learning about JavaFX layouts.

General advice

The Oracle site has useful layout tutorials there and practice.

It is usually better to use automated layout managers, rather than absolute positioning. The automatic layout managers assist in creating GUIs within areas of different sizes with support for dynamic resizing.

Use SceneBuilder to learn about layouts

Download scene builder from Gluon.

Use SceneBuilder to quickly mock up different layouts.

Use the preview feature to try the layouts out to see how they respond to interaction and resizing.

Export the SceneBuilder output to fxml and controller skeletons and inspect the output carefully to understand how the visual design maps to code.

It is much easier and more efficient to create and mock-up layouts in SceneBuilder than in Java code.

But don’t rely only on SceneBuilder for learning about layouts, also write layout logic in Java code so you know how to do that.