I've been playing around with swing for some time and decided to check out FX now. So far I'm finding it a lot easier and more fun to work with as compared to swing but I've run into a small speed bump and after hours of looking around I just can't find a solution.
I am unable to use \u when I try to add it through the fxml file It works fine if I don't use the fxml but I want to use the scene builder as it is more convenient.
Here's the small piece of code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label layoutX="145.0" layoutY="129.0" text="\u0644\u0627\u062B\u0627\u0646\u0649" />
</children>
</AnchorPane>
The error I keep getting is
Caused by: javafx.fxml.LoadException: Invalid escape sequence.
Not sure if relevant, but I'm using jdk1.8.0_131 & netbeans 8.2
If anyone could point me in the right direction here I'd really appreciate it.
FXML is an XML, and so you need to use XML escaping:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label layoutX="145.0" layoutY="129.0" text="لاثانى" />
</children>
</AnchorPane>
That being said, if you are able to input the characters, you can just insert them as is.
See also: https://www.w3.org/International/questions/qa-escapes
Related
I created a Spinner with JavaFx accepting Integer only. The user choose a number between 0 and 10 (number of turns he decides to rest ingame).
However, I want to get this value when the user clicks on the "OK" button.
Here is the line where I want to call the user input. (I tried .getValue() but it's not working) :
#Override
public void initialize(URL url, ResourceBundle rb) {
RestOK.setOnAction(e -> ToF.rest((int) RestSpinner.getValue(), restTime));
}
And this is the fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cc.view.RestController">
<children>
<Label layoutX="66.0" layoutY="46.0" text="How many turns do you need for resting?" />
<Separator layoutX="81.0" layoutY="83.0" prefWidth="240.0" />
<Spinner fx:id="RestSpinner" editable="true" layoutX="94.0" layoutY="109.0">
<valueFactory>
<SpinnerValueFactory.IntegerSpinnerValueFactory max="10" min="0" />
</valueFactory>
</Spinner>
<Button fx:id="RestCancel" cancelButton="true" layoutX="170.0" layoutY="160.0" mnemonicParsing="false" onAction="#closeRest" text="Cancel" />
<Button fx:id="RestOK" layoutX="276.0" layoutY="109.0" mnemonicParsing="false" text="OK" />
</children>
</AnchorPane>
Thank you
I'm trying to convert some working JavaFX code to FXML. I had tried the following, but I get an error:
java.lang.IllegalArgumentException: No enum constant javafx.scene.layout.Priority.Always
<?import javafx.scene.layout.VBox ?>
<?import javafx.scene.layout.HBox ?>
<?import javafx.scene.layout.Pane ?>
<?import javafx.scene.layout.Priority ?>
<VBox xmlns:fx="http://javafx.com/fxml" >
<HBox>
<Button text="License" />
<Button text="Credits" />
<Pane HBox.hgrow="Always"></Pane>
<Button text="Exit" />
</HBox>
</VBox>
I then tried it a different way, which works, however I don't understand why the first one does not, while the second one does. I've seen code examples of the first way on here, has something changed and the code samples are too old, am I missing some import, or have I completely failed to understand what I'm doing?
<?import javafx.scene.layout.VBox ?>
<?import javafx.scene.layout.HBox ?>
<?import javafx.scene.layout.Pane ?>
<?import javafx.scene.layout.Priority ?>
<VBox xmlns:fx="http://javafx.com/fxml" >
<HBox>
<Button text="License" />
<Button text="Credits" />
<Pane>
<HBox.hgrow><Priority fx:value="ALWAYS" /></HBox.hgrow>
</Pane>
<Button text="Exit" />
</HBox>
</VBox>
James_D above got it, turns out I had a typo. Sometimes extra eyes catch stupidity.
Changed
<Pane>
<HBox.hgrow><Priority fx:value="ALWAYS" /></HBox.hgrow>
</Pane>
To
<Pane HBox.hgrow="ALWAYS"></Pane>
I am new with JavaFX FXML and I was trying fx:script with this "NMAESTAGE.fxml" file below:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?language javascript?>
<VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER" prefWidth="200.0">
<children>
<Label prefHeight="17.0" prefWidth="83.0" text="Control name:" />
<Region prefHeight="25.0" prefWidth="0.0" HBox.hgrow="ALWAYS" />
<TextField />
</children>
</HBox>
<HBox prefHeight="26.0" prefWidth="228.0">
<children>
<Region HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="nextAction()" prefHeight="25.0" prefWidth="53.0" style="-fx-background-radius: 0; -fx-background-color: rgb(0,110,215);" text="Next" textFill="WHITE">
<fx:script>
function nextAction(){
root.getScene().getWindow().hide();
}
</fx:script>
</Button>
</children>
</HBox>
</children>
<padding>
<Insets bottom="30.0" left="30.0" right="30.0" top="50.0" />
</padding>
</VBox>
But the problem is, when I try to open the fxml file in scene builder, the fx:script tag is causing a NullPointerException. It says that "make sure NAMESTAGE.fxml is a valid fxml file".
The application compiles, builds, and runs without any error.
Removing the 5 lines with fx:script tag along with the onAction="nextAction()" part, somehow solve the problem. I don't understand what I am doing wrong here. Could anybody help?
For me it's a bug, introduced by Scene Builder 2.0; the older versione (1.1) doesn't have the problem
I have got an FXML file in which there is a custom component declared. This component has some logic that requires a reference to another component declared in the same fxml file. The FXML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane xmlns:fx="http://javafx.com/fxml" prefWidth="800" prefHeight="400" fx:controller="test.MyController">
<top>
<HBox spacing="10">
<Label text="Test"/>
<TextField fx:id="testField"/>
</HBox>
</top>
<center>
<AnchorPane>
<children>
<VBox fx:id="customBox" visible="false" prefHeight="400" prefWidth="400"
VBox.vgrow="ALWAYS"
initiator="#testField"/>
</children>
</AnchorPane>
</center>
</BorderPane>
So, I need to set the initiator property of customBox. With this, I am getting Caused by: java.lang.IllegalArgumentException: Unable to coerce #testField to class javafx.scene.control.TextField. Any suggestions?
I don't have time to test this, but I think
<KontrollkoderVBox fx:id="customBox" visible="false" prefHeight="400" prefWidth="400"
VBox.vgrow="ALWAYS"
initiator="$testField"/>
will work. See "variable resolution" in the FXML documentation.
You can even do things like binding a StringProperty in KontrolkoderVBox to the TextField's textProperty() by referencing ${testField.text}.
You may set the id of textField (say id="testFieldId" which is different from fx:id) into initiator and do lookup:
// in VBox #customBox
TextField tf = (TextField) getScene().lookup(getInitiator());
where getInitiator() returns String = "#testFieldId".
i'm trying to build a JavaFX Application and i want to attach the CSS file in the FXML file but when i try to run the app i get a problem like: Property "stylesheets" does not exist or is read-only. in the console i don't know what is the problem exactly (note if i delete the CSS file everything works fine).
The CSS file "csstyle.css" contains:
/*
* Empty Stylesheet file.
*/
.mainFxmlClass {
-fx-background-color: blue;
}
Also i wrote in the Stylesheet in the FXML file the name of the CSS file also in the Style Class : .mainFxmlClass .
So i want to know the error please help me and thanks :)
You have two problems:
The first one is already solved here. Remove
stylesheets="#csstyle.css"
and add it right after the <children> tags of your root container. Since you don't show how is your FXML file, this is how it should look like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import java.net.*?>
<AnchorPane styleClass="mainFxmlClass" ...>
<children>
...
</children>
<stylesheets>
<URL value="#cssstyle.css" />
</stylesheets>
</AnchorPane>
The second is this one:
styleClass=".mainFxmlClass"
Remove the dot:
styleClass="mainFxmlClass"
EDIT
Given these solutions didn't work and the fact that the OP is using JDK 7 with JavaFX 2.0 these other solutions may help:
Replace:
styleClass="mainFxmlClass"
with:
<styleClass>
<String fx:value="mainFxmlClass"/>
</styleClass>
So your FXML will be like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import java.net.*?>
<AnchorPane ...>
<children>
...
</children>
<styleClass>
<String fx:value="mainFxmlClass"/>
</styleClass>
<stylesheets>
<URL value="#cssstyle.css" />
</stylesheets>
</AnchorPane>
And try it again.
Finally, if it is not working, remove the <stylesheets> tags, and set the stylesheet file on your Scene:
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("cssstyle.css").toExternalForm());
So your FXML will be like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<AnchorPane ...>
<children>
...
</children>
<styleClass>
<String fx:value="mainFxmlClass"/>
</styleClass>
</AnchorPane>
It's really advisable updating JavaFX to newer versions (2.2.45 is the last one for Java 7), and benefit from many new features added.