FXML Label text bold - javafx

Can someone please tell me how to make the text inside a label bold in Java FXML?
<Label fx:id="LegalLabel" text= "Legal Information" GridPane.halignment="RIGHT" GridPane.rowIndex="16" />
Thank you.

Give this a shot
legalLabel.setStyle("-fx-font-weight: bold;");
or try making your fxml look like this
<Label fx:id="LegalLabel" text= "Legal Information" GridPane.halignment="RIGHT" GridPane.rowIndex="16">
<font>
<Font name="System Bold" size="13.0" />
</font>
</Label>
Edit: try this: legalLabel.setFont(Font.font("Verdana", FontWeight.BOLD, 12));

Related

Random characters instead of String input in Combobox Items?

I am trying to create a choicebox in my application using javafx/scenebuilder in IntelliJ but so far am not able to get Strings to show as menu items - in the example screenshot below I have just tried to create an option with the letter C? The code I have used is below. Any ideas what I am doing wrong? I thought it might be a font issue but I can't find a way to change the font of the menu items.
<AnchorPane prefHeight="45.0" prefWidth="127.5">
<children>
<Text fill="WHITE" strokeType="OUTSIDE" strokeWidth="0.0" text="I" AnchorPane.leftAnchor="11.0" AnchorPane.topAnchor="4.0">
<font>
<Font name="Courier Bold" size="40.0" />
</font>
</Text>
<ChoiceBox fx:id="keyChoice" prefWidth="79.0" style="-fx-background-color: #FBC5B8;" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="8.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="C" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
So this was a font issue as suspected, couldn't find a way to change it in FXML so created a CSS file which works fine.
.choice-box .menu-item .label {
-fx-font-family: "monospace";
}
.choice-box {
-fx-font-family: "monospace";
}

Retrieving strings from textfields in a .fxml file when a button is clicked. (creating a login scene to switch scenes to home scene)

very new to programming. I am learning java and javaFX.
I have tried to make a login scene and a home scene and currently have a "login" button that invokes a method in the controller -loginButton()- that switches between the two scenes. The part I am unsure on is how to set up this method to retrieve the strings from the username and password text/password fields.
I am not interested just yet in setting this up with a database. what I want to go for is something like this (rubbish login details for the purpose of example, I would never actually use login info this bad):
public void loginButton(javafx.event.ActionEvent actionEvent) throws IOException {
if(textFromLoginField=="admin"){
if(textFromPasswordField=="Password123"){
//run code to change scene
}
}
}
I know how to switch scene but I dont know how to get the login info (shown as textFromLoginField and textFromPasswordField in the example above.
How I am switching scenes
Node node=(Node) actionEvent.getSource();
Stage stage=(Stage) node.getScene().getWindow();
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("homeScene.fxml")),750,500));
stage.show();
My FXML text:
VBox maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="159.0" prefWidth="157.0" spacing="10.0" style="-fx-background-color: Black;" HBox.hgrow="NEVER">
<HBox.margin>
<Insets bottom="60.0" top="60.0" />
</HBox.margin>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<Label text="LOGIN:" textFill="WHITE" />
<TextField fx:id="loginTextField" promptText="Username" />
<PasswordField fx:id="passwordTextField" promptText="Password" />
<HBox alignment="TOP_RIGHT" prefHeight="21.0" prefWidth="137.0" spacing="10.0">
<children>
<Button mnemonicParsing="false" fx:id="loginbutton" onAction="#loginButton" style="-fx-background-color: Blue;" text="Login" textAlignment="CENTER" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button mnemonicParsing="false" fx:id="helpbutton" onAction="#helpButton" style="-fx-background-color: Orange;" text="Help" textAlignment="CENTER" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</HBox>
</children>
</VBox>
Any help greatly appreciated :)
-Also, if I have used any technical vocab incorrectly please let me know. I am new to this. thanks
"==" doesn't work with Strings. Use textFromLoginField.equals();. Check out the string functions.

Using FontAwesomeFX, how to have two icons for one button?

In my JavaFX program I am using FontAwesomeFX to add icons to buttons, labels, etc.
The way how I style this is through the .fxml file:
<Button fx:id="btnGoToWeb"
onAction="#btnGoToWeb">
<tooltip>
<Tooltip text="Go to Web"/>
</tooltip>
<graphic>
<FontAwesomeIconView glyphName="GLOBE" size="1.6em"/>
</graphic>
</Button>
Producing this button:
I would like to know how I can add 2 icons to one button. I would like to have something like shown below (please note the image is modified to illustrate the desired output):
Per #Slaw comment.
<Button mnemonicParsing="false" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<graphic>
<HBox alignment="CENTER" spacing="5.0">
<children>
<FontAwesomeIconView />
<FontAwesomeIconView />
</children>
</HBox>
</graphic>
<tooltip>
<Tooltip text="Go to Web" />
</tooltip>
</Button>

JavaFX TextArea does not append text properly

I'm trying to build a simple calculator using TDD, so I have a TextArea that is right aligned to display the results and 20 buttons. Everytime one of the digit buttons are pressed it just appends the digit to the TextArea, the same thing was supposed to happen when I press the dot button. But instead it places the dot as the first character in the TextArea, only when I press a new digit the dot goes to its proper place.
So for example, if I press 9 and then 8 the TextArea shows "98", now if the dot is pressed the result will be ".98", finally if 7 is pressed the result is "98.7".
The expected result when I pressed the dot button should have been "98.".
I created a Minimal, Complete, and Verifiable example bellow
FXMLDocument.fxml:
<AnchorPane id="AnchorPane" prefHeight="175.0" prefWidth="256.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="mcv.FXMLDocumentController">
<children>
<Button fx:id="buttonNine" layoutX="14.0" layoutY="100.0" onAction="#handleButtonNine" prefHeight="60.0" prefWidth="69.0" text="9">
<font>
<Font size="24.0" />
</font></Button>
<TextArea fx:id="textArea" editable="false" focusTraversable="false" layoutX="15.0" layoutY="14.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="66.0" prefWidth="221.0" wrapText="true">
<font>
<Font size="35.0" />
</font>
</TextArea>
<Button fx:id="buttonDot" layoutX="169.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleButtonDot" prefHeight="60.0" prefWidth="69.0" text=".">
<font>
<Font size="24.0" />
</font>
</Button>
</children>
</AnchorPane>
FXMLDocumentController.java:
#FXML
private TextArea textArea;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
private void appenToResult(String ch){
textArea.appendText(ch);
}
#FXML
void handleButtonDot(ActionEvent event) {
appenToResult(".");
}
#FXML
void handleButtonNine(ActionEvent event) {
appenToResult("9");
}
As you can see the code is very simple. It should be working, my only clue is that this is a bug with the TextArea when the Node Orientation is set to RIGHT_TO_LEFT. If node orientation is set to INHERIT or LEFT_TO_RIGHT it works as expected. Thank you.

JavaFX element is initialize with size 0

I have a problem when my FXML are initialized and theirs values are listen by listeners.
So my main window is a BorderPane
TOP = menu.fxml
CENTER = center1.fxml
BOTTOM = footer.fxml
When I click on one of buttons on center1Controller the CENTER fxml should changed to center2.fxml.
In center2.fxml I have a Pane that should be Anchor.anchorLeft=60 and anchorTop = anchorBottom
I was created an listener :
public void addTopOffsetListener() {
this.logonWindowPane.heightProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double y = (newValue.doubleValue() - logonForm.getHeight()) / 2;
logonForm.setLayoutY(y);
}
});
}
But one my screen is reload the logonForm is in bad place. When I debug moment the initialization getHeight() returns 0.
It is placed in correct place after resizing window.
I was trying fixed size of this element on FXML and on CSS.
Actually I was find an workaround. When I initialize a screen2Controller a I have added this line
public void initialize(URL url, ResourceBundle rb) {
this.logonForm.resize(300, 400);
}
There is my Screen2 fxml :
<AnchorPane id="logonWindowPane" fx:id="logonWindowPane" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonWindowController">
<stylesheets>
<URL value="#../styles/application_logon.css" />
</stylesheets>
<children>
<Pane id="logonForm" fx:id="logonForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400" styleClass="projectPane" AnchorPane.rightAnchor="60.0" />
</children>
</AnchorPane>
And the FXML of element there I put in logonForm :
<Pane styleClass="logonFXForm" prefHeight="400" prefWidth="300" minWidth="300" minHeight="400" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genealogytree.application.fxmlcontrollers.LogonFormController">
<stylesheets>
<URL value="#../styles/application_logon.css" />
</stylesheets>
<children>
<JFXPasswordField focusColor="#1f7f6a" layoutX="38.0" layoutY="133.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Password" unFocusColor="#253930">
<font>
<Font size="14.0" />
</font>
</JFXPasswordField>
<JFXTextField focusColor="#1f7f6a" layoutX="38.0" layoutY="54.0" maxWidth="224.0" minWidth="224.0" prefHeight="48.0" prefWidth="224.0" promptText="Login" unFocusColor="#253930">
<font>
<Font size="14.0" />
</font>
</JFXTextField>
<JFXButton buttonType="RAISED" graphicTextGap="8.0" layoutX="61.0" layoutY="224.0" prefHeight="48.0" prefWidth="178.0" ripplerFill="#32ae8f" styleClass="jfxButton" text="CONNECT" textAlignment="CENTER">
<font>
<Font name="System Bold" size="18.0" />
</font>
</JFXButton>
<Hyperlink layoutX="38.0" layoutY="305.0" prefHeight="25.0" prefWidth="250.0" text="Do not have an Account ? " />
<Hyperlink layoutX="38.0" layoutY="340.0" prefHeight="25.0" prefWidth="250.0" text="Forgotten Password ? " />
</children>
</Pane>
But there is another method to resolve my problem in FXML or CSS only ?

Resources