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.
Related
Picture of my application now
I would like to have "Tools" in the menubar to align right but I've searched a lot on the internet and could not find anything useful so I'm trying it here. The next thing I wanted to fix and did not find is Gridpane which is not filling its parent's size. If any of you know the properties or code to make this happen. Let me know!
public MyMenuBar(StackPane root) {
menu1 = new Menu("KunstwerkLijst");
menuItem1 = new MenuItem("Lijst van kunstwerken");
menuItem1.setOnAction(event ->{
root.getChildren().clear();
new Home(root);
});
menu2 = new Menu("Tools");
menuItem2 = new MenuItem("Admin Panel");
menuItem2.setOnAction(event ->{
root.getChildren().clear();
// new APanel(root);
});
menu1.getItems().addAll(menuItem1, menuItem4, menuItem6, menuItem5);
menu2.getItems().addAll(menuItem2, menuItem3);
this.getMenus().add(menu1);
this.getMenus().add(menu2);
Right align menu in menubar in JavaFX
This should help you for your first problem. If not you could maybe replace the menuubar with a toolbar and do something like this:
How to right align a button in Java FX toolbar
For your second problem it depends on what the actual parent of the Gridpane is.
Using an AnchorPane as parent is the easiest solution here and then use it like this:
<AnchorPane fx:id="parent">
<GridPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
...
</GridPane>
</AnchorPane>
I set a tooltip on a text element in JavaFx, everything works as long as I do not hover mouse on the text, the tooltip does not get displayed.
<Text layoutX="329.0" layoutY="202.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Tcond[C]" textAlignment="CENTER">
<Tooltip text="'%Empty_Bundle'"/>
</Text>
That's the fxml code which should create a tooltip on this text.
How to fix it without creating assign text to an object in my view controller and setting on the tooltip by using .setTooltp() method?
In FXML, if you embed one instance element directly inside another, without a property element, it is used to set the property annotated as the #DefaultProperty on the outer instance element.
If you check the documentation for Text you will see that the default property is "text": so your FXML is equivalent to
<Text layoutX="329.0" layoutY="202.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Tcond[C]" textAlignment="CENTER">
<text>
<Tooltip text="'%Empty_Bundle'"/>
</text>
</Text>
In other words, you are trying to set the text of your Text object to the tooltip.
Text doesn't actually have a setTooltip(...) method at all; that method is defined in the Control class, of which Text is not a subclass. The only way to set a tooltip on a Text instance is by using the Tooltip.installTooltip(...) method, which you can only call from the controller (there is no FXML equivalent to calling this static method).
Your other option, if it works, would be to replace the Text with a Label:
<Label layoutX="329.0" layoutY="202.0" text="Tcond[C]" textAlignment="CENTER">
<tooltip>
<Tooltip text="'%Empty_Bundle'"/>
</tooltip>
</Label>
Note that Label does not allow you to directly configure the stroke, as a Text does, so you would lose that functionality (or at least have to use CSS instead).
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.
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.
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 :(