-3

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);
  • 2
    Getting stuck is normal, I wouldn’t worry about it, instead, go to the Oracle site and study the [layout tutorials](https://docs.oracle.com/javase/8/javafx/layout-tutorial/index.html) there and practice. – jewelsea Sep 24 '21 at 22:31
  • 2
    Also get your terms right. You are placing nodes in the scene graph, not elements on a canvas, so the title doesn’t make sense. In JavaFX, a canvas has a completely different meaning. Run a Google searches on “JavaFX scene graph” and “JavaFX canvas” to understand how different they are. – jewelsea Sep 24 '21 at 22:38
  • 2
    *”I want to use HBox to place a button at a specific (x,y) coordinate.”*. That’s simply not what an `HBox`, or any other layout pane, does. [Layout panes](https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm) position child nodes relative to each other according to the specific rules of that pane; in the case of an `HBox` nodes are laid out horizontally in a row. It’s almost always bad practice to hard-code layout positions anyway. Just use a layout pane (or combination thereof) that positions the components where you want. – James_D Sep 24 '21 at 22:39

1 Answers1

1
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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406