I have a custom component that is extending the HBOX. Am adding a VBOX and a "HBOX" to it as its children.The VBOX has a label which acts as title to component and adding/deleting a pair of LINK BUTTONS to the "HBOX" dynamically on a particular event(might be a button click).
The parent HBOX width must grow/shrink whenever the link button is added/deleted dynamically.
How to calculate the same.I do not know.
you can set width and height dinamically with this:
var hb:HBox;
hb.percentHeight = 100; //height:100%
hb.percentWidth = 100; //widht:100%
You can set width with percent (100%) or bind width property to another width property (in mxml with {}).
Related
Is it possible to override a JavaFX CSS definition in code?
For example, in my CSS file I defined a button's height as follows:
.button {
-fx-pref-height: 30px;
}
When I try to override this height for some button in code...
Button button = new Button();
button.setPrefSize(100, 50); // width, height
...the button still has a height of 30 pixels.
There are two solutions:
Make sure that button.setPrefSize(100, 50); // width, height is called after the Button became visible.
Use Bindings to force its size (CSS does not overwrite bound values), for example: button.prefHeightProperty().bind(new SimpleDoubleProperty(60));
I am waiting to expand and contract a linear layout.
The linear layout height will be set to 0dp pixels in the beginning and once a user selects a certain button then that layout height will be expanded to MATCH_PARENT.
Once the user selects the button again the linear layout is given a height of 0dp again.
Please any help will be awesome. Thank you.
You may try something like below to set the height to 0
// Get your linearlayout
LinearLayout layout = (LinearLayout)findViewById(R.id.mylinearlayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 0;
I have these buttons with different size:
Image
How I can make all buttons with same with size?
It depends on layout where the button is located. For example, if you add all the buttons into GridPane or BorderPane, you have to specify each button width to correspond to certain variable. In the following example I wrap all buttons inside VBox, set VBox preference width and tie up all buttons minimum width to it:
VBox vBox = new VBox();
vBox.setPrefWidth(100);
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMinWidth(vBox.getPrefWidth());
btn2.setMinWidth(vBox.getPrefWidth());
vBox.getChildren().addAll(btn1, btn2);
It is also worth to mention that there are two ways to specify the button size. You can do it in the java code or specify it in javafx .fxml file. The above method is an example for java code implementation.
You can also unclamp a button's maximum dimensions so it will grow to fill the available space (unlike most nodes, by default a button node has it's max size clamped to it's preferred size so it doesn't usually grow to fill available space). An Oracle tutorial on Tips for Sizing and Aligning Nodes explains this in more detail.
VBox vBox = new VBox();
Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");
btn1.setMaxWidth(Double.MAX_VALUE);
btn2.setMaxWidth(Double.MAX_VALUE);
vBox.getChildren().addAll(btn1, btn2);
using css you can override the preferred width of all buttons like
.button {
-fx-pref-width: 200px;
}
or create your own style class for certain button groups and add the style to the button like:
css:
.my-special-button {
-fx-pref-height: 28px;
-fx-pref-width: 200px;
}
and then set the style to your button with either
fxml:
styleClass="my-special-button"
or in java
myButton.getStyleClass().add("my-special-button");
Is it possible to manage child elements in a HBox, so that the sum of the widths of all child elements is equal to the width of the HBox?
So that elements fill the HBox and no space is left.
The height of the child elements is by default the height of the HBox, so how about the width? I don't want to calculate the width in my program. It would be better if the layout does this automatically, so that no calculation is needed.
It depends what kind of children does the HBox contain. Some of the children may not be resizable nodes. However, generally speaking, you can use HBox.setHgrow() method and set the same Priority for all children of hbox. The explanation is in its javadoc:
Sets the horizontal grow priority for the child when contained by an
hbox. If set, the hbox will use the priority to allocate additional
space if the hbox is resized larger than it's preferred width. If
multiple hbox children have the same horizontal grow priority, then
the extra space will be split evening between them. If no horizontal
grow priority is set on a child, the hbox will never allocate it
additional horizontal space if available. Setting the value to null
will remove the constraint.
Additionally, if you are trying to obtain a grid-like layout then try out other layout options, for instance TilePane or FlowPane and maybe GridPane.
The simple code for your answer would be this:
Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);
It's recommended to use HBox class rather than name of HBox pane.
In FXML:
<HBox>
<Label HBox.hgrow="ALWAYS" maxWidth="Infinity">
TEST
</Label>
</HBox>
Adding to #bdshahab's answer, since HBox will try to resize their children to their preferred widths, they will only fill the box if their combined preferred widths are wider than the HBox width.
So it may be necessary to do something like:
Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);
button1.setPrefWidth(100000);
button2.setPrefWidth(100000);
I have a component ObjectHolder which extends UIComponent that holds many children UIComponents. However, after using addChild() to add these children to ObjectHolder, its width and height remain zero. Is it possible for the UIComponent to automatically expand to the size of its containing children components?
public class ObjectHolder extends UIComponent
{
public function ObjectHolder()
{
var c1:UIComponent = new UIComponent();
c1.graphics.beginFill( 0xffffff );
c1.graphics.drawRect(0, 0, 100, 100);
addChild( c1 );
trace( this.width ); // Displays zero
}
}
To change the component's size you should implement measure() method. But you should also follow other recommendations on creating custom components. In your case you should create and add child component in createChildren() method.
Is it possible for the UIComponent to automatically expand to the size
of its containing children components?
The formal answer is no. A component is never responsible for setting it's own height and width values; it is only responsible for sizing and positioning it's children based on the values set by that component's parent.
If you implement a measure() method in your component, you can use it to set the measuredWidth and measuredHeight of your component. This will, in essence, be a suggestion that you provide to your parent's container on what is the ideal size you need to position and size all your child elements properly. Your own components should be sized and positioned in updateDisplayList() based on the unscaledWidth and unscaledHeight values passed in.
Most of the time the Flex default containers will honor these sizing values; although there are situations where they will not be. For example, if the size the child container needs is more than the size of the parent.