I know it's possible to do that in FXML:
<Spinner fx:id="spinner" min="0" max="100" initialValue="3" >
<editable>true</editable>
</Spinner>
But I can't find how it's possible to do that in sceneBuilder
I got the same problem, I didn't found Spinner in Scene Build and I put in Scenne Builder manually. This is my code.
I have modified .fxml document to put the Spinner in Pane.
<Pane fx:id="paneTamano" layoutX="527.0" layoutY="46.0" prefHeight="125.0" prefWidth="141.0" stylesheets="#css.css">
<children>
<Spinner fx:id="spinnerTamano" layoutX="17.0" layoutY="49.0" onMouseClicked="#tamano" prefHeight="25.0" prefWidth="110.0" />
</children>
</Pane>
I created an ObservableList at DocumentController.java to give values to the spinner.
ObservableList<String> listaTamanos = FXCollections.observableArrayList("Grande", "Mediana", "Pequeña");
Finally insert values in to the Spinner.
SpinnerValueFactory<String> valueFactory = new SpinnerValueFactory.ListSpinnerValueFactory<String>(listaTamanos);
spinnerTamano.setValueFactory(valueFactory);
valueFactory.setValue("Pequeña");
Spinner was introduced in JavaFX 8. It has no Stylesheet handling like the one in (for ex.) Slider implemented right now, so SceneBuilder don't know the properties that are stylable.
Compare Spinner and for example Slider source code, there is missing a nested StyleableProperties class in Spinner.
Related
I am working on th GUI of an JavaFX application which shall show/hide some GUI elements due to identification of the logged user.
I would like to prepare this by defining properties programmatically which are written to the namespace of the fxml loader. This works fine, but unfortunately not on the fxml in the fx:include tags.
Is it possible to access the namespace of the loaders of the included fxml (if so: how?), or should I renounce the use of fx:include here?
Any recommendation is highly appreciated.
Thank you in advance.
in Java:
Properties prop = new Properties();
prop.setProperty("some_key1", "some_value1");
prop.setProperty("some_key2", "some_value2");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/fxml.fxml"));
prop.stringPropertyNames()
.forEach(key -> loader.getNamespace().put(key, prop.getProperty(key)));
in the parent FXML:
<Label fx:id="label" maxHeight="20.0" maxWidth="1.7976931348623157E308" minHeight="30.0" text="${some_key1}"/> /* here some_value1 appears */
<TabPane id="tabPane" fx:id="tabPane" minHeight="860.0" tabMaxHeight="30.0" tabMaxWidth="200.0" tabMinHeight="30.0" tabMinWidth="100.0">
<Tab id="tab" fx:id="Tab" closable="false" disable="true" text="Text">
<fx:include fx:id="tabContent" source="tab-content.fxml" />
</Tab>
</TabPane>
In the child FXML:
<Label fx:id="label" maxHeight="20.0" maxWidth="1.7976931348623157E308" minHeight="30.0" text="${some_key2}"/> /* here some_value2 does not appear */
I created ChoiceBox using scene builder 2.0 and attached to IntelliJ Idea and add below code to add item to ChoiceBox in Main class
ChoiceBox genderBox = (ChoiceBox) scene.lookup("#gender");
genderBox.setItems(FXCollections.observableArrayList("Boy", "Girl"));
genderBox.getSelectionModel().select(0);
Choice box code from FXML is as below
<ChoiceBox id="gender" prefHeight="16.0" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="11">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</ChoiceBox>
Anyway, when running the Main class I see the choice box as below. choice box width is not matched with context menu(popup) width.
How to set a width to the context menu?
EDIT
Anyway setting width to the context menu in combo box is working fine. So I moved with combo box. see example fxml.
First, set width of the ChoiceBox itself...
<ChoiceBox prefWidth="150">
...
</ChoiceBox>
Then, set width of the popup list via CSS...
.choice-box .menu-item > .label {
-fx-pref-width: 100;
}
note: the box is about 50px wider than the list because of the drop-down button.
I have control that extends HBox called TablePagination that I created purely in code that I want to include in a FXML file, so when I do this:
<VBox fx:id="box" spacing="15" styleClass="sectionStyle">
<StackPane>
<TablePagination fx:id="pagination" StackPane.alignment="CENTER"/>
</StackPane>
</VBox>
nothing appears. But when I do it in code like this:
pagination = new TablePagination(itemTable,items);
StackPane pane = new StackPane();
pane.setAlignment(pagination, Pos.CENTER);
pane.getChildren().add(pagination);
box.getChildren().add(pane);
My control gets rendered but not in center. So what am I missing?
In your code version TablePagination is centered within StackPane, but nodes inside TablePagination are not. Call:
pagination.setAlignment(Pos.CENTER);
Note that StackPane.setAlignment method is static, and you sould call:
StackPane.setAlignment(pagination, Pos.CENTER);
I have a SplitPane with a TreeView on the left and a content area on the right. When I click on an item in my TreeView I want to display content on the right. How I do this now is to load an FXML which I create in SceneBuilder. My problem is that the FXML doesn't fit to the SplitPane. This is how I load the FXMl file
if (selectedItem.getValue() == "Sample") {
try {
AnchorPane pane = (AnchorPane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
splitPane.getItems().set(1, pane);
} catch (IOException e) {
e.printStackTrace();
}
}
How do I make this AnchorPane which I created to fit to the original SplitPane size?
I have been struggling with this as well,
I finally solved by problem by avoiding nested wrapping of AnchorPanes.
I've removed the AnchorPane as the child container of the SplitPane, because the root of the included FXML also starts with an AncherPane:
Now my main FXML has something like:
<SplitPane>
<items>
<fx:include fx:id="navigationPanel" source="FX2NaviPanel.fxml"/>
<fx:include fx:id="dataPanel" source="FX2dataPanel.fxml"/>
</items>
</SplitPane>
and each child FX2NaviPanel.fxml and FX2dataPanel.fxml still start with an AnchorPane:
<AnchorPane ...>
<children>
<GridPane></GridPane>
</children>
</AnchorPane>
With this and appropriate maximum sizes, and fit-to-parent and zero-anchors where relevant the panels nicely stick to the movement of the splitter and resizing of the main window.
Aligning in FX is a pain i.t.a.
I suggest the following:
<SplitPane>
<TreeView /> //LEFT
<ANCHORPANE AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> //RIGHT, anchors make the content stick to the corner
<AnchorPane fx:id="myDynamicContent"/>
</ANCHORPANE>
</SplitPane>
With this the inner pane myDynamicContent should be stretched to all corners. Please tell if this is the answer.
You can also try to set the AncorPane.* attributes ON your content so you dont need nested AnchorPane.
Edit: After thinking about it I think you need the outer pane because it is "THE CONTAINER", without it the inner pane does not what to stick to.
Sample FXML with the BorderPanel.alignment "static property":
<BorderPane>
<top>
<Label text="My Label" BorderPanel.alignment="CENTER"/>
</top>
</BorderPane>
The CSS-supported version:
<BorderPane stylesheets="Style.css">
<top>
<Label text="My Label" styleClass="labelClass"/>
</top>
</BorderPane>
Style.css would be:
.labelClass
{
-fx-borderpanel-alignement: center
}
For JavaFX versions 2.0 to 2.2 => no you cannot set the static layout properties via css.
You can create a feature request in the JavaFX jira to ask this functionality be implemented in a future JavaFX version.
You can apply it programatically. For example if you want to center all the widgets of a GridPane, instead of writing GridPane.halignment="CENTER" in the XML declaration of each widget, you can do this in Java :
for(Node node : gridPane.getChildren())
{
GridPane.setHalignment(node, HPos.CENTER);
}
Unfortunately I don't think you can factorize layouts as in Android :(