Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX alignment of Label in GridPane

I'm confused as to why setting the Label.setAlignment(Pos.CENTER_RIGHT) does not affect the alignment of a label that is then added into a GridPane? The only way to do it is seemingly through the grid (e.g. ColumnConstraints) or by e.g. adding the Label to a HBox that has right alignment.

Why does setting the alignment of the label to CENTER_RIGHT have no effect? I can see the API says: "Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled." But how do I get empty space in a label?

like image 688
Tranquility Avatar asked Feb 16 '16 16:02

Tranquility


People also ask

How the nodes in a grid pane are arranged?

If we use Grid Pane in our application, all the nodes that are added to it are arranged in a way that they form a grid of rows and columns. This layout comes handy while creating forms using JavaFX.


1 Answers

TL:DR version: instead of label.setAlignment(Pos.CENTER_RIGHT); use GridPane.setHalignment(label, HPos.RIGHT);.

JavaFX uses a top-down layout. So the scene sizes the root node to the size of the scene, the root node sizes and positions each of its child nodes depending on its layout strategy and the amount of space the scene gave it, each child node then sizes and positions its child nodes depending on its own layout strategy and the amount of space it has available, and so on down the scene graph.

According to the documentation for Label, the alignmentProperty

Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.

Of course, the amount of space available to the label is determined by its parent, which in this case is the grid pane. You can of course find out about the grid pane's layout strategy and how to configure it by reading its documentation. In brief, though:

By default, a grid pane will allocate each node its preferred size, and, if the cell it's placed in has additional space, will align the node in the top left of the grid cell. The preferred size for a label is of course the computed size: just big enough to hold its content. Hence you see that simply placing a label into a grid pane and setting alignment on the label will not have any effect, because the label is sized just large enough to hold its content, and there is no extra space to align the text/graphic. You can visualize this by placing a background color or border on the label.

So you could set the alignment on the label to CENTER_RIGHT, and then instruct the grid pane to let the label grow. This needs two things: first, tell the grid pane to let the label fill the width of the cell:

GridPane.setFillWidth(label, true);

and, since the grid pane will also respect the child node's maximum size, and the label by default uses its preferred size as its maximum size, allow the label to grow:

label.setMaxWidth(Double.MAX_VALUE);

Then the label will grow to the size of the cell, and so if you also have

label.setAlignment(Pos.CENTER_RIGHT);

it will align its own content to the right.

A more sensible approach is probably just to tell the grid pane how to align the label in the cell:

GridPane.setHalignment(label, HPos.RIGHT);

Then let the label take on its default size and alignment and everything will work.

You can also use a ColumnConstraints object to set the default alignment for all labels in a particular column, which is by far the more convenient approach if you are building a form.

like image 103
James_D Avatar answered Sep 29 '22 06:09

James_D