javafx label cant fill the free space - javafx

I would like to achieve this layout in HBox
|[Message ]|[Ok]|[Cancel]|
But I cant force the label to fill the free space at left, and I get this
| [Message]|[Ok]|[Cancel]|
The HBox.hgrow=ALWAYS doesnt help.
The code is something like that below:
<HBox alignment="CENTER_RIGHT">
<children>
<Label fx:id="lblMessage" ... HBox.hgrow="ALWAYS" />
<Button fx:id="btnOk" ... HBox.hgrow="NEVER" />
<Button fx:id="btncancel" ... HBox.hgrow="NEVER" />
</children>
</HBox>
How can I get the label to fill the left space ?

The default maximum width of a Label is its preferred width. Setting the max width to a suitably large value should solve your issue.

Related

I have chosen a Pane, but I would like to set the alignment, is it possible?

I'm doing a project with javafx. As cointainer I choose the Pane, maybe the worst decision! Is it possible to center the text, using the lenght of the sentence which will be loaded according to the size of the panel?
I'm actually showing the code that i wrote. As you can see, the GeneralQuestion lenght will be various, the question has not a min or max character. How can i center it according to the size of the panel?
This is how does it look like:
This is the output, as you can see it isn't centered. I know there's the VBox or other type of panel but i've already chosen this one and before changing everything i would like to know if there's any way to make it look better!
This is the controller:
public void nextQuest(ActionEvent e) throws IOException
{
secondaryImg.setVisible(false);
nextQuestionButton.setVisible(false);
getQuestion(cont);
cont++;
}
public void getQuestion(int cont) throws FileNotFoundException,
IOException
{
FileManager f = new FileManager();
numQuest();
QuestionManager m = new QuestionManager(f.loadQuestion());
GeneralQuestion.setText(m.getAllQuestion().get(cont).getBody());
answ=m.getAllQuestion().get(cont).getAnswer();
if(m.getAllQuestion().get(cont).getType().equals("normale")||m.getAllQuestion().get(cont).getType().equals("perditutto"))
answS.setVisible(true);
else if(m.getAllQuestion().get(cont).getType().equals("verofalso")){
RbVero.setVisible(true);
RbFalso.setVisible(true);
}
}
This is (a part of) the FXML:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #b8ffe0; -fx-border-width: 8px; -fx-border-color: #0e3d73;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="guiz.controller.GameScreenController">
<children>
<Label id="numquest" fx:id="numquest" layoutX="286.0" layoutY="35.0" text="Label">
<font>
<Font size="15.0" />
</font></Label>
<Label fx:id="GeneralQuestion" layoutX="225.0" layoutY="123.0" style="-fx-font-weight: 800;" text="Quest">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</Pane>
In JavaFX you achieve a desired layout by nesting various layout panes inside each other. Pane is indeed a bad descision because a Pane just does not do any layout at all. In your case a VBox would indeed be a better alternative which you might put into a BorderPane.

Avoid having to update all rowIndexes every time I insert a new row in the middle of a GridPane?

If I have lots of rows and I feel like inserting a new one at the top (or at any other position) I then have to go and manually increment the GridPane.rowIndex field, which gets tiresome quite easily.
Is there any symbol I can put instead of the hardcoded numbers to make FXML understand that they're just a sequence?
...
<!-- inserting anything here will make me have to pudate the rowIndex of all
<!-- rows below it. quite tiresome?
<Text text="Trade ID:" GridPane.rowIndex="0" GridPane.columnIndex="0" />
<TextField fx:id="tradeIdField" GridPane.rowIndex="0" GridPane.columnIndex="1" editable="false" />
...
Thanks

JavaFX: Node re-size is not working as expected

I have defined an FXML with a GridPane of 8 rows, which holds the following components (FXML and CSS definitions are given at the end of this thread):
2 CheckBoxes,
2 Labels,
2 Labels With a Region and
2 Buttons with A Region
The Region here is used to load images into, which is done through a CSS style. For each set of the components defined above (for example the 2 Checkboxes), I take the one and I increased it's size by using the -fx-font-size styling. In particular, I define the font size to be "1.3xDefault_System_Font_Size (12)". The tags (INC) and (DEF) in text of each component, is used to describe whether the font size is increased or left to the default value.
As you can see from the above screenshot, the label of checkbox is increased but the box itself did not. The same applies for the images, that their size did not increase but the label size did.
This gets worse when I change the default font size in .root:
.root{
-fx-font-size: 28;
}
In the example above, since I changed the default font size from 12 to 28, I expect all labels and images to change their size according to this new size. The image size is defined by the -fx-background-size which is 4em.
However, all the labels are increased correctly but the images are increased correctly only on those nodes with the DEF tag. The same applies for the checkbox, where it's label and box is increased on the DEF, whilst the one with the INC tag only label size increased correctly and the box remained small.
Is there a way to make images and box of checkbox to increase their size correctly? Is this a bug, or am I doing something wrong?
FXML: Gridpane
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane stylesheets="#css/Test.css" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ebay.client.controller.SettingsScreenController">
<children>
<CheckBox nodeOrientation="RIGHT_TO_LEFT" styleClass="increased_font" text="CheckBox (Inc)" GridPane.rowIndex="1" />
<CheckBox nodeOrientation="RIGHT_TO_LEFT" text="CheckBox (Def)" GridPane.rowIndex="2" />
<Label cache="true" styleClass="increased_font" text="Label (Inc)" GridPane.rowIndex="3" />
<Label cache="true" text="Label (Def)" GridPane.rowIndex="4" />
<Label cache="true" styleClass="increased_font" text="Label With Image (Inc)" GridPane.rowIndex="5">
<graphic><Region styleClass="javasuns_logo" /></graphic>
</Label>
<Label cache="true" text="Label With Image (Def)" GridPane.rowIndex="6">
<graphic><Region styleClass="javasuns_logo" /></graphic>
</Label>
<Button styleClass="increased_font" text="Button With Image (Inc)" GridPane.rowIndex="7">
<graphic><Region styleClass="javasuns_logo" /></graphic>
</Button>
<Button text="Button With Image (Def)" GridPane.rowIndex="8">
<graphic><Region styleClass="javasuns_logo" /></graphic>
</Button>
</children>
</GridPane>
CSS: Test.css
.increased_font {
-fx-font-size: 1.3em;
}
.javasuns_logo {
-fx-font-size: null;
-fx-pref-height: 2em;
-fx-pref-width: 4em;
-fx-background-image: url('../../icons/logo/javasuns.png');
-fx-background-size: 4em;
-fx-background-repeat: no-repeat;
-fx-background-position: center;
}
What I found is that when you're using pref-height and pref-width you cannot use -fx-font-size in the same class cause it "resets" the .root -fx-font-size to the default value of 12.
I wanted to set specific font-size in some buttons as well as setting the width and height using the em values. It wasn't possible so I created a Label inside the element of Button and here I can resize the label font-size correctly. So if I don't use the font-size and pref-width/height in the same class/same control it looks like it's working.

Graphic and text position in a button (JavaFX) [duplicate]

I was wondering How one can achieve following button style where text resides underneath of image in JavaFx?
I tried a lot but all in vain. Any help would be appreciated.
The key is the contentDisplay property, set it to "TOP".
With fxml:
<Button contentDisplay="TOP" layoutX="101.0" layoutY="51.0" mnemonicParsing="false" text="Button">
<graphic>
<ImageView mouseTransparent="true" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#image.png" preserveRatio="false" smooth="false" />
</image>
</ImageView>
</graphic>
</Button>
Or CSS:
.your-selector {
-fx-content-display: top;
}
Check the CSS reference here.
You can simply achieve that just by doing
Button b = new Button("text", graphics);
b.setContentDisplay(ContentDisplay.TOP);
And for cool icons, take a look at http://fxexperience.com/controlsfx/features/ it include icone from FontAwesome and IcoMoon

How to set the position of an ImageView in a TitledPane title?

I've solved the problem to bring an icon image into the header of a TitledPane like this:
<TitledPane fx:id="x2" text="Strukturen">
<graphic>
<ImageView >
<image>
<Image url="#/de/myres/icons/arrow-out.png" />
</image>
</ImageView>
</graphic>
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0"
prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
This looks like the following:
As you can see, it looks not very nice when the arrow for collapsing and this icon are shown beside each other directly.
How can i achieve to get the icon to the right side of the title bar?
You could leave the TitledPane text blank and set the icon node to be an HBox containing a Label on the left and an ImageView on the right.

Resources