ComboBox FXML default value - java

How can I set a default value in a ComboBox using FXML?
<ComboBox fx:id="cbo_Bacteriologie_Aesculine" prefHeight="21.0" prefWidth="105.0" GridPane.columnIndex="1" GridPane.rowIndex="0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="NVT" />
<String fx:value="Bezig" />
<String fx:value="Positief" />
<String fx:value="Negatief" />
</FXCollections>
</items>
</ComboBox>
I want NVT to be selected by default. I tried adding selected="selected" and such but don't seem to find the right syntax.
Is it possible to edit the listed items using Scene Builder? I can't seem to find it.

Use this:
<ComboBox>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="NVT" />
<String fx:value="Bezig" />
<String fx:value="Positief" />
<String fx:value="Negatief" />
</FXCollections>
</items>
<value>
<String fx:value="NVT" />
</value>
</ComboBox>

I don't think it's possible in the FXML. You will need to do it in the initialization of the component, in the controller, for example using the following line cbo_Bacteriologie_Aesculine.getSelectionModel().setSelectedIndex(1); for selecting the element Bezig.
But if you find a way to do it in FXML, I am interested.
EDIT : It is possible in FXML. You can see it in Guedolino's answer (https://stackoverflow.com/a/14436371/1344424), which should become the right answer to this question.

I got a strange error with the first suggested method
setSelectedItem(T) has protected access in SelectionModel
where T is a type-variable:
T extends Object declared in class SelectionModel
For me
getSelectionModel().select("NVT");
worked like a charm.

Related

Why get unsupported child for menu exception in zk?

I have this zk code:
<zk>
<window apply="org.zkoss.bind.BindComposer" viewModel="#id('vm')#init('IndexViewModel')" validationMessages="#id('vmsgs')">
<menubar>
<menu>
<menuitem label="${each}" forEach="#load(vm.menupoints)" />
</menu>
</menubar>
</window>
</zk>
in my view model:
private List<String> menupoints = Arrays.asList("Home", "ShipTracking");
//getter-setter
But i get a index.zul: org.zkoss.zk.ui.UiException: Unsupported child for menu: exception...what can be a problem? My goal is to create a menu with different content...may be can anybody help with it also?
Thanks!
Menuitem is not a supported child of menu.
If you look at the documentation you see that it only supports menupopup
<menu>
<menupopup>
<menuitem label="${each}" forEach="#load(vm.menupoints)" />
</menupopup>
</menu>

JavaFX SegmentedButton - Buttons full width

I got a SegmentedButton which contains 3 "glyph only" ToggleButtons like this:
<SegmentedButton maxWidth="Infinity" prefWidth="Infinity">
<buttons>
<fx:define>
<ToggleGroup fx:id="albumViewToggleGroup"/>
</fx:define>
<ToggleButton maxWidth="Infinity" fx:id="tagCloudToggle" mnemonicParsing="false" selected="true" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="TAGS"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="gridFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup" >
<graphic>
<Glyph fontFamily="FontAwesome" icon="TH"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="coverFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="ELLIPSIS_H"></Glyph>
</graphic>
<VBox.margin>
<Insets top="10.0"/>
</VBox.margin>
</ToggleButton>
</buttons>
</SegmentedButton>
The SegmenedtButton consumes the full width (represented by the red line), though the ToggleButtons are not. I checked this by setting a background color.
I would like that the ToggleButtons are stretched so that they are each 1/3 of the width of the SegmentedButton. How can i achive this?
According to the documentatiom mathematical operators can be used in fxml bindings.
So something like this can be used:
<SegmentedButton fx:id="segButton" maxWidth="1.7976931348623157E308" >
<buttons>
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K3" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K2" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1,1" />
</buttons>
</SegmentedButton>
Probably no longer needed, but should hopefully be useful for anyone who winds up googling this.
I doubt that you still need help with this problem, but for anyone like me, who googled the problem and got here, here's how I solved it:
button1.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
button2.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
segmentedButtons.getButtons().addAll(button1, button2);
segmentedButtons.setMaxWidth(Double.MAX_VALUE);
So basically, for every ToggleButton that you add to SegmentedButton you bind preferred width to the actual width of SegmentedButton divided by the number of buttons. Since it's binding, the width will adjust on resizing the window, so you need to do it only once on creation. And if you want, you can divide width in some other way to make some buttons bigger and some smaller.

JavFX 2 MenuButton

I'm trying to create a MenuButton with multiple items. Then, when one of the items is clicked, grab it's value. But can't figure out how to do this?
Here is a snippet from my FXML:
<MenuButton fx:id="changeStatusButton" layoutX="420.0" layoutY="16.0" mnemonicParsing="false" onAction="#changeStatusFired" prefHeight="26.0" prefWidth="180.0" text="Change My Status" AnchorPane.rightAnchor="17.0">
<items>
<MenuItem mnemonicParsing="false" text="Set Available" />
<MenuItem mnemonicParsing="false" text="Set Unavailable" />
</items>
</MenuButton>
How would I go about grabbing the text from the selected item? So if the first item is selected, I would like to get the text "Set Available" to perform an action based on that. But can't figure out how to do that? For a textfield this is easy, but have no idea how these MenuButtons work. All the information I've found only tells you how to populate them, but I've already pre-populated them...
Menus and MenuButtons don't maintain a "selectedItem" state. The MenuItems fire ActionEvents when the user clicks on them (or invokes their action via a keyboard shortcut). So you need to register action listeners for them:
<MenuButton fx:id="changeStatusButton" layoutX="420.0" layoutY="16.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="180.0" text="Change My Status" AnchorPane.rightAnchor="17.0">
<items>
<MenuItem mnemonicParsing="false" onAction="#setAvailable" text="Set Available" />
<MenuItem mnemonicParsing="false" onAction="#setUnavailable" text="Set Unavailable" />
</items>
</MenuButton>
and then in your controller:
private boolean available ;
// ...
#FXML
private void setAvailable(ActionEvent event) {
available = true ;
}
#FXML
private void setUnavailable(ActionEvent event) {
available = false ;
}
If you want true selection functionality, you should consider using a ComboBox. Obviously in this example a CheckBox would be the best option, but I assume your real example has more choices.

Load page in a tab with ZK

I have this code in ZK, with a menu to navigate:
<zk apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #init('menu')">
<tabbox width="100%" sclass="nav_tabs">
<tabs id="tabs">
<tab label="Admin" onSelect="updateCategory(self.label)"/>
<tab label="User" onSelect="updateCategory(self.label)"/>
</tabs>
<tabpanels>
<tabpanel>
<toolbar hflex="true">
<toolbarbutton label="CreateUser" onClick="#Command('load',label=self.label)" />
<toolbarbutton label="CreateMno" onClick="#Command('load',label=self.label)" />
</toolbar>
</tabpanel>
<tabpanel>
<toolbar hflex="true">
<toolbarbutton label="LoadData" onClick="#Command('load',label=self.label)" />
<toolbarbutton label="DownloadData" onClick="#Command('load',label=self.label)" />
</toolbar>
</tabpanel>
</tabpanels>
</tabbox>
<separator height="30px"></separator>
<zscript><![CDATA[
void updateCategory(String category) {
current_category.setValue(category);
current_subpage.setValue("Index");
}
]]></zscript>
<hlayout>
<label id="current_category" sclass="nav_text nav_category" onClick="#command('submit')">Our Product</label>
<label sclass="nav_text">-</label>
<label id="current_subpage" sclass="nav_text">Index</label>
</hlayout>
</zk>
Then i have two roles Admin, and User, and i need load the pages of each user, and i am trying that when a user click in the toolbarbutton, for example CreateUser, then call a method that load in the space of my toolbar button the page, but i do not how can i do it.
Something like:
http://www.zkoss.org/zkdemo/tabbox/navigation_tabs
but i not need a string in Our product -> Product 1 - > Our Product-Product 1, i need load a page, and i am calling the page with the same name to the label.
And a page from a java class
Somebody can help me?
Please add the following to your tabpanel:
<include id="includeID" mode="instant" src="page.zul" />
Later on, you may load another page by adding the following to a zscript:
includeID.setSrc("another_page.zul");

Custom FXML Annotations

For some additional/custom attributes, like tab indices or tool tip texts, it would be nice to annotate the FXML with custom annotations. I have three proposals for it:
XML-Comment:
<!-- #TabIndex(index=1) -->
<Button fx:id="test_audio" text="Test" />
fx attribute:
<Button fx:id="test_audio" fx:tab_index="1" text="Test" />
Custom XML namespace attribute :
<Button fx:id="test_audio" custom:tab_index="1" text="Test" />
Is there a way to do this and get the attribute values on runtime?

Categories