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>
Related
I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored.
Add a pane with no content which always grows to fit available space between the left aligned tools in the bar and right aligned ones.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<ToolBar prefHeight="40.0" prefWidth="318.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<Button text="Apples" />
<Button text="Oranges" />
<Pane HBox.hgrow="ALWAYS" />
<Button text="Help" />
</ToolBar>
(Verified in Scene Builder 11.0.0)
One option: Use two ToolBar containers wrapped with an HBox container. Put the Help button in the right ToolBar. Put the left-aligned buttons in the left toolbar. For the left ToolBar, set the HGrow to ALWAYS. For the Right ToolBar, set HGrow to NEVER. For each ToolBar, set all Sizes to USE_COMPUTED_SIZE.
Here is the relevant fxml:
<HBox VBox.vgrow="NEVER">
<children>
<ToolBar HBox.hgrow="ALWAYS">
<items>
<Button text="Apples" />
<Button text="Oranges" />
</items>
</ToolBar>
<ToolBar HBox.hgrow="NEVER">
<items>
<Button text="Help" />
</items>
</ToolBar>
</children>
</HBox>
This is the hierarchy as displayed in Scene Builder:
This is the display within Scene Builder:
If you could place ur button inside a stack pane then u could make use of Stackpane's alignment property that takes javafx.geometry.Pos - The alignment of the child within the stackpane.For example in ur case:
<StackPane >
<Button translateY="-15" translateX="15" StackPane.alignment="TOP_RIGHT"/>
</StackPane>
BorderPane mainBorderPane = new BorderPane();
BorderPane ToolBorderPane = new BorderPane();
ToolBar tBarLeft=new ToolBar();
ToolBar tBarRight=new ToolBar();
ToolBorderPane.setLeft(tBarLeft);
ToolBorderPane.setRight(tBarRight);
mainBorderPane.setTop(ToolBorderPane);
...
...
for your left aligment items add tBarLeft and
your right aligment items add tBarRight
It can be done with some hack: apply 180 degree rotation around Y-axis for the toolbar and all its buttons.
<ToolBar rotate="-180.0" HBox.hgrow="ALWAYS">
<items>
<Button mnemonicParsing="false" rotate="180.0" text="Button">
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</Button>
</items>
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</ToolBar>
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.
I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored.
Add a pane with no content which always grows to fit available space between the left aligned tools in the bar and right aligned ones.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<ToolBar prefHeight="40.0" prefWidth="318.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<Button text="Apples" />
<Button text="Oranges" />
<Pane HBox.hgrow="ALWAYS" />
<Button text="Help" />
</ToolBar>
(Verified in Scene Builder 11.0.0)
One option: Use two ToolBar containers wrapped with an HBox container. Put the Help button in the right ToolBar. Put the left-aligned buttons in the left toolbar. For the left ToolBar, set the HGrow to ALWAYS. For the Right ToolBar, set HGrow to NEVER. For each ToolBar, set all Sizes to USE_COMPUTED_SIZE.
Here is the relevant fxml:
<HBox VBox.vgrow="NEVER">
<children>
<ToolBar HBox.hgrow="ALWAYS">
<items>
<Button text="Apples" />
<Button text="Oranges" />
</items>
</ToolBar>
<ToolBar HBox.hgrow="NEVER">
<items>
<Button text="Help" />
</items>
</ToolBar>
</children>
</HBox>
This is the hierarchy as displayed in Scene Builder:
This is the display within Scene Builder:
If you could place ur button inside a stack pane then u could make use of Stackpane's alignment property that takes javafx.geometry.Pos - The alignment of the child within the stackpane.For example in ur case:
<StackPane >
<Button translateY="-15" translateX="15" StackPane.alignment="TOP_RIGHT"/>
</StackPane>
BorderPane mainBorderPane = new BorderPane();
BorderPane ToolBorderPane = new BorderPane();
ToolBar tBarLeft=new ToolBar();
ToolBar tBarRight=new ToolBar();
ToolBorderPane.setLeft(tBarLeft);
ToolBorderPane.setRight(tBarRight);
mainBorderPane.setTop(ToolBorderPane);
...
...
for your left aligment items add tBarLeft and
your right aligment items add tBarRight
It can be done with some hack: apply 180 degree rotation around Y-axis for the toolbar and all its buttons.
<ToolBar rotate="-180.0" HBox.hgrow="ALWAYS">
<items>
<Button mnemonicParsing="false" rotate="180.0" text="Button">
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</Button>
</items>
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</ToolBar>
Take for example the menu items from the edit menu in JavaFX Scene Builder
See how they display the shortcuts on the right? Is there any easy way to achieve the same effect using JavaFX? Thanks.
You can add an accelerator key in scene builder or add it directly in the fxml file like so
<?import javafx.scene.input.*?>
...
<MenuItem mnemonicParsing="true" onAction="#mnuSaveAction" text="%menu.title.save" fx:id="mnuSave">
<accelerator>
<KeyCodeCombination alt="UP" code="S" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
KeyCodeCombination has two constructors, second example:
<MenuItem text="Renommer..." onAction="#rename" mnemonicParsing="true">
<accelerator>
<KeyCodeCombination code="F2"><modifiers></modifiers></KeyCodeCombination>
</accelerator>
</MenuItem>
If by "in javafx" you mean without using fxml you can use mnuSave.setAccelerator(KeyCombination);
If your are not using fxml file then you can add key combination like below.
MenuItem gotoLine = new MenuItem("Goto");
KeyCombination gotoKeyCombination = new KeyCodeCombination(KeyCode.G, KeyCombination.CONTROL_DOWN);
gotoLine.setAccelerator(gotoKeyCombination);
gotoLine.setOnAction(event -> {
System.out.println("CTRL+G event triggered");
System.out.println(event.toString());
});
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.