How do I add sub-headers to my menu using JavaFX and the fxml files? I have looked at the CustomMenuItem option but I am unable to figure out what to put in the content part. Thanks for your help!
My fxml file:
<?import javafx.scene.input.*?>
<?import javafx.scene.control.*?>
<MenuBar>
<menus>
<Menu text="Menu 1">
<items>
<MenuItem text="Item 1" />
<MenuItem text="Item 2" />
<MenuItem text="Item 3" />
<SeparatorMenuItem />
<MenuItem text="Item A" />
<MenuItem text="Item B" />
<MenuItem text="Item C" />
</items>
</Menu>
</menus>
</MenuBar>
Below is an example of the result I am looking for. "Header 1" and "Header 2" shouldn't be clickable and should not highlight when the mouse moves over them.
Thanks for your input. Based on the link you provided, I found that it simply can be done adding the following in the fxml file:
<SeparatorMenuItem>
<content>
<Text text="Header Name" styleClass="textSeparator" />
</content>
</SeparatorMenuItem>
I think the answer you're looking for is here:
http://tiwulfx.panemu.com/2013/01/02/creating-custom-menu-separator-in-javafx/
Related
I am trying to create a choicebox in my application using javafx/scenebuilder in IntelliJ but so far am not able to get Strings to show as menu items - in the example screenshot below I have just tried to create an option with the letter C? The code I have used is below. Any ideas what I am doing wrong? I thought it might be a font issue but I can't find a way to change the font of the menu items.
<AnchorPane prefHeight="45.0" prefWidth="127.5">
<children>
<Text fill="WHITE" strokeType="OUTSIDE" strokeWidth="0.0" text="I" AnchorPane.leftAnchor="11.0" AnchorPane.topAnchor="4.0">
<font>
<Font name="Courier Bold" size="40.0" />
</font>
</Text>
<ChoiceBox fx:id="keyChoice" prefWidth="79.0" style="-fx-background-color: #FBC5B8;" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="8.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="C" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
So this was a font issue as suspected, couldn't find a way to change it in FXML so created a CSS file which works fine.
.choice-box .menu-item .label {
-fx-font-family: "monospace";
}
.choice-box {
-fx-font-family: "monospace";
}
I want to use the same MenuItem in a MenuBar and in a ContextMenu (both have the same text, will be enabled/diSabled under the same conditions, and will perform the same task). How can I achieve this in FXML in the same file?
I tried using <fx:reference> but it only shows up in the first place I deference it.
<MenuBar>
<menus>
<MenuItem fx:id="menuToDuplicate" />
</menus>
</MenuBar>
.
.
.
<ContextMenu>
<items>
<!--Same MenuItem as above -->
</items>
</ContextMenu>
I have defined the combo Box in javaFX FXML file now i want to define it's function to get the value of Combo Box and use it in my code.
The FXML file is
<ComboBox fx:id="select_pc" promptText="Select PC">
<HBox.margin>
<Insets left="20.0" top="35.0" />
</HBox.margin>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="ForkLift" />
<String fx:value="Gates" />
</FXCollections>
</items>
</ComboBox>
now can anybody tell me how to write a function to take the value of combo Box and use it in the String form
I have declared a menu bar in FXML with a bunch menu items (containing graphics, onClick method links etc...).
Now I'm creating a context menu for a table, and I'd like to put in there all the menu items for the "Edit" menu of the menu bar.
Is there a DRY way of doing so in FXML?
I don't like the idea of copying all the FXML declarations of the menu items, and having to maintain both sets of items.
I know I could reuse the items if I declared them in Java code, but I'd like to keep all my layout in FXML.
Here is the FXML for the edit menu, that I don't want to duplicate:
<Menu text="_Edit">
<MenuItem onAction="#copyRaw" text="Copy _raw log">
<accelerator>
<KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
<graphic>
<Glyph fontFamily="FontAwesome" icon="copy" />
</graphic>
</MenuItem>
<MenuItem onAction="#copyPretty" text="Copy with _columns">
<accelerator>
<KeyCodeCombination alt="UP" code="C" control="DOWN" meta="UP" shift="DOWN" shortcut="UP" />
</accelerator>
<graphic>
<Glyph fontFamily="FontAwesome" icon="copy" />
</graphic>
</MenuItem>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem onAction="#selectAll" text="Select _All">
<accelerator>
<KeyCodeCombination alt="UP" code="A" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
<MenuItem mnemonicParsing="false" onAction="#unselectAll" text="Unselect All" />
</Menu>
I'm not sure that my idea is best but u should to do it like this:
You can create a package in resources with most used elements as Buttons,Tables,Labels etc. for each assign an fx:id and include that in another fxmls.
Example:
PACKAGE: resource/package/name/utils/Label.fxml
<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<Label xmlns:fx="http://javafx.com/fxml"
fx:id="label"
style="-fx-background-color: red;
-fx-font: bold;
-fx-font-size: 30px;"
text="Hello world">
<padding>
<Insets top="5" left="5" right="5" bottom="5"/>
</padding>
</Label>
<!--Add all your nodes there-->
PACKAGE: resource/package/name/Main.fxml
<GridPane xmlns:fx="http://javafx.com/fxml" alignment="TOP_CENTER" hgap="10" vgap="10">
<fx:include source="Label.fxml"/> <!--There will be displayed all elements from Label.fxml-->
</GridPane>
In your case you just need to set fx:id for your Menu item and import in another fxml.
Good luck.
How can I develop a 2 level horizontal menu in flex 3? I want a sub menu to appear when you mouse over the 1st level.
The Menu control has this functionality built in. Here's Adobe's online documentation with some examples for you to check out.
Example:
<?xml version="1.0"?>
<!-- menus/SimpleMenuControl.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
<![CDATA[
// Import the Menu control.
import mx.controls.Menu;
// Create and display the Menu control.
private function createAndShow():void {
var myMenu:Menu = Menu.createMenu(null, myMenuData, false);
myMenu.labelField="#label";
myMenu.show(10, 10);
}
]]>
</mx:Script>
<!-- Define the menu data. -->
<mx:XML format="e4x" id="myMenuData">
<root>
<menuitem label="MenuItem A" >
<menuitem label="SubMenuItem A-1" enabled="false"/>
<menuitem label="SubMenuItem A-2"/>
</menuitem>
<menuitem label="MenuItem B" type="check" toggled="true"/>
<menuitem label="MenuItem C" type="check" toggled="false"/>
<menuitem type="separator"/>
<menuitem label="MenuItem D" >
<menuitem label="SubMenuItem D-1" type="radio"
groupName="one"/>
<menuitem label="SubMenuItem D-2" type="radio"
groupName="one" toggled="true"/>
<menuitem label="SubMenuItem D-3" type="radio"
groupName="one"/>
</menuitem>
</root>
</mx:XML>
<mx:VBox>
<!-- Define a Button control to open the menu -->
<mx:Button id="myButton"
label="Open Menu"
click="createAndShow();"/>
</mx:VBox>
</mx:Application>