I made an anchorpane with an fxml and a controller. I also made a borderpane with a view, fxml and another controller. How can I add the anchorpane inside the borderpane? The reason I want them in separate fxml and controller is that the borderpane is a lot of code and this would make everything look cleaner.
I thought it would work like this but it doesn't.
Borderfxml:
?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.textfield.*?>
<BorderPane fx:controller="Bordercontroller" xmlns:fx="http://javafx.com/fxml"
prefHeight="200" prefWidth="320">
<top>
</top>
<left>
</left>
<center>
</center>
<right>
<Anchorpane fx:id="Same id as my anchorpane"/>
</right>
<bottom>
</bottom>
</BorderPane>
Related
I am creating a small application in javafx. I want to set a background color so following is my FXML code .. how I need to set background color
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLhomepageController">
<children>
<Label fx:id="lb1" layoutX="139.0" prefHeight="109.0" prefWidth="343.0" text="home page" textAlignment="JUSTIFY" textFill="#355680">
<font>
<Font size="68.0" />
</font>
</Label>
</children>
</AnchorPane>
Here is an example:
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: blue;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" />
If you are using Scene Builder, you should learn how to use this section under Properties:
You can also use a CSS stylesheet to accomplish this.
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.
In my application I defined the GUI in FXML. There is a label that always show the version of java. Is it possible to make it show this information without coding it in the controller?
I am thinking something like this:
<Label fx:id="info" fx:value="$java.version"/>
But this of course does not work. Is there a way?
So you don't want to set it via controller class.
Then you can utilize javascripting feature of FXML:
<?language javascript?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.FXMLDocumentController">
<fx:script>
var myText = 'version: ' + java.lang.System.getProperty('java.version');
</fx:script>
<children>
<Label text="$myText" />
</children>
</VBox>
I have a SplitPane with two items: progress bar (top pane) and action buttons (e.g., ok/cancel) as the bottom pane.
When the page is load, both panes are displayed.
During initial load, I want to have the top pane hidden and the bottom pane displayed.
The top pane will be display when an action event occurs via button click.
How should I modify the code to be able to execute the described scenario
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import com.ngc.gs.dialogs.widgets.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane fx:id="progressMainAnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="390.0002999999924" prefWidth="398.9998779296875" xmlns:fx="http://javafx.com/fxml" fx:controller="com.ngc.gs.dialogs.widgetexample.MyCheckBoxSelController">
<TreeView fx:id="localTreeView" prefHeight="181.0" prefWidth="363.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="14.0" />
<SplitPane fx:id="progressSplitPane" focusTraversable="true" maxWidth="1.7976931348623157E308" orientation="VERTICAL" prefHeight="181.0" prefWidth="362.999755859375" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="195.0">
<items>
<ProgressBarMessageConsoleWidget id="pBarConsoleWidget" fx:id="pBarConsoleWidget" minHeight="150.0" minWidth="0.0" prefHeight="150.0" prefWidth="361.0" />
<OkCancelButtonsWidget id="actionButtonsWidget" fx:id="actionButtonsWidget" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
</items>
</SplitPane>
<stylesheets>
<URL value="#../styles/dialogs.css" />
</stylesheets>
</AnchorPane>
Controller class snippet that sets the splitPane divider:
....
progressSplitPane.setDividerPosition(0, 0.0);
progressSplitPane.setResizableWithParent(progressMainAnchorPane, true);
actionButtonsWidget.getOkBtn().setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent evnt) {
// Show progress bar and message console
progressSplitPane.setDividerPosition(0, 0.75);
pBarConsoleWidget.showProgressConsole();
processCheckBoxSelections();
}
});
...
I resolved this by implementing a GridPane instead and adding listener to the property as described here...
JavaFX HBox hide item