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?
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.
Related
My widget has two layouts. The main layout is outerLayout, and it should always occupy the entire widget. This contains a QLabel and a second layout named innerLayout.
innerLayout should be centered inside outerLayout and it should only be large enough to contain its widgets. I've set size constraints for both layouts, but in the following code, innerLayout expands to fill outerLayout.
outerLayout = new QVBoxLayout();
outerLayout->addWidget(label);
outerLayout->setSizeConstraint(QLayout::SetMaximumSize);
innerLayout = new QGridLayout();
innerLayout->setAlignment(Qt::AlignHCenter);
innerLayout->setSizeConstraint(QLayout::SetFixedSize);
outerLayout ->addLayout(innerLayout);
setLayout(outerLayout);
If I set the size constraint for both layouts to SetFixedSize, both layouts will be reduced to their minimum size. But I want the outer layout to occupy the entire space and the inner layout to use the minimum possible space. Any ideas?
Use spacers as extra widgets of outerlayout. In other words: the spacers will be siblings of innerlayout and will push inner layout to it's minimal needed surface. You will need horizontal and vertical spacers.
I have a tableview cell that contains a horizontal UIStackView. Inside it is an UIImageView (on the left, 100x100 dimensions) and a vertical UIStackView (on the right) that contains 4 labels. The vertical UIStackView is constrained to the UIImageView's top and bottom anchors. But the bottom constraint is "less than or equal to".
I am setting the text property on these labels but if the text is nil I hide that label. Stack View than sets the height of that label to 0 and it doesn't appear in the Stack View. Now the idea is if some labels are hidden, the combined intrinsic content size's of the remaining labels will define the height of the vertical UIStackView and it will decrease in height based on that "less than or equal to" bottom constraint.
This doesn't work though. Take the case where the 2nd and 4th labels have been hidden. The 1st label is normal height. But the 3rd label stretches all the way down to the bottom of the UIImageView. This is in contrast to my expectation that the intrinsic content sizes of the labels would be less and the label's UIStackView would shrink.
I'm guessing there is some priorities that might need to be tuned to get this working properly. This whole view hierarchy is contained in a UITableViewCell and that has dynamic height applied which I hope doesn't cause any problems.
So if the intended behavior is that when I hide labels the vertical UIStackView shrinks, what constraints must I add/delete or which priorities must be set?
I have a GridPane displaying descriptive Labels on the left side and content Labels on the right side. I set everything to USE_COMPUTED_SIZE in SceneBuilder and added padding to my elements so they don't stick together too closely. This works well if it wasn't for the fact, that the left-side Labels are cut off, if the content is big enough to take up the whole width of the scene:
Is there a way of making sure that my left-side labels are displayed in full, before the right side labels get their share of available width? Setting the minimum width of the first column to a concrete value works, but i would like JavaFX to determine the needed size for the first column.
Use Texts (can be found in Shapes) instead of Labels in the first column. In contrast to Labels Texts are not resizeable and GridPane cannot resize the first column to become smaller than the largest of the Text nodes in this column.
I know I can fix this programmatically and I know I could set the text to tighten / scale but I would like to know how to get this text to extend organically to a third line on small screens. How can I accomplish that?
There are a couple of things you need to take care of before your label grows:
Make sure the 'number of lines' field in the attributes inspector is set to zero. Setting it zero allows the label to grow depending upon the content it has.
If the label is in a container view, make sure you haven't specified the height constraint explicitly on the container view. Since the container view should generate its height from its subviews and the subviews will generate their height from the content they have. Its sort of a chain process that goes on if you have a deeper hierarchy.
Make sure there is no sibling view to the container view with an explicit height that might cause your container view to shrink while maintaining its own height. This point may also apply even if your label is not within some container view.
In the image below, the container view(gray one) is bound from three sides allowing it to grow from the bottom.
Below image shows the constraints applied to the content views of the container. The container is driving its height from its content views.
Below I have increased the text of the label from a single line to three lines. At this point the label tries to expand horizontally but since the container view is bind with the super view on both sides the label has only one direction left to increase itself. It increases downwards pushing the textfield and button down and since the button is tied with the bottom of the container view it pulls the container view increasing its height.
I'm having trouble using the layout manager system with Qt. This is going to be a Symbian app, so it should resize to different devices.
This is done by using the Layouts.
On the image below I used the Vertical Layout, but I don't understand how I can decide how much each cell should take in width and height.
I want the blue to be a top label background, but I don't want it to be as high as it is now.
Does anyone know how I can do this? (I'm new to Qt :))
You can set the maximum size for a widget by right clicking it and selecting 'Size Constraints'. Under that menu you can find actions that allow you to set the current displayed size as the maximum / minimum size for vertical / horizontal or both directions.
You can also set the numbers by hand by selecting the widget and by setting the number in the 'Property Editor'. They should be under the QWidget properties.
You cannot set the Height of a vertical layout directly, but you can set the height of the widget in which the vertical layout is.
If you want to split your Widgets so that the top widget takes 33.33% of the space, use the Stretch values. Set the top widget to 1 and the bottom widget to 2.