Javafx: How to Stretch Menu item Width - javafx

As you can see below how the menu items are stretched, I want to do the same.

The key to this is adding a KeyCombination to menuItem.setAccelerator(). The Menu should automatically stretch when you add KeyCombinations It will stretch based on your longest MenuItem.
Programmatically:
MenuItem menuItem = new MenuItem("action");
menuItem.setAccelerator(new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN));
Menu menu = new Menu("File");
menu.getItems().add(menuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(menu);
FXML:
<MenuItem mnemonicParsing="true" text="action">
<accelerator>
<KeyCodeCombination alt="UP" code="T" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>

<MenuBar>
<Menu text="Menu Item">
<items>
<MenuItem text="New" style="-fx-padding: 0 70 0 70"/>
<SeparatorMenuItem/>
<MenuItem text="Exit" style="-fx-padding: 0 70 0 70"/>
</items>
</Menu>
</MenuBar>

This is kind of old, but for the future visitors: you can do this by using css file like this:
.menu-item {
-fx-padding: 5 5 80 5
}
You can play with the numbers to see what it changes.

Related

How to use the same component in more than one place in JavaFX?

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>

Material-ui - nested menu - change css width of menuItems[] of a menuItem

<Menu desktop={true} autoWidth={false} style={{width: "120px", overflowX: 'hidden'}}>
<MenuItem desktop={true} rightIcon={<ArrowDropRight />} primaryText="Option1" menuItems={[
<MenuItem desktop={true} primaryText="Option1a" onTouchTap={props.ontheclick}/>,
<MenuItem desktop={true} primaryText="Option1b" onTouchTap={props.ontheclick}/>]}/>
<MenuItem onTouchTap={props.ontheclick} primaryText="Option 2" />
</Menu>
I have a material ui menu like above. I want to set style like I'm doing in Menu for MenuItems in menuItems={[MenuItem, MenuItem]}. I've tried adding style={{width: "120px", overflowX: 'hidden'}} to <MenuItem desktop={true} primaryText="Option1a"/> but it doesn't change the actual width of the nested menu. Right now, my main menu is way thinner than my nested menu. How to do this?

Reuse the same FXML declaration for menu bar and context menu

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 to add sub-headers to menu using fxml files?

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/

How to open a menu automatically with a shortcut?

I have a MenuBar which contains general menu items like File, View, Tools ,Help.
I have sub items in each of those menuitems.
The problem is that i want to open the 'File' menu automatically when i press Alt+f key.
I could capture the keyevents on the view.
But how to open the File Menu of the MenuBar (what is the function that needs to be called from MenuBar Class to popup those sub menuitems) ?
I have searched for some information on google .. but cudnt find.
or else if u have any better solution or example ..plz do post it.
<root>
<menuitem label="File">
<menuitem label="New" enabled="false"/>
<menuitem label="Open.." enabled="false"/>
<menuitem label="Save" enabled="false"/>
<menuitem label="Restore" enabled="false"/>
<menuitem label="Print" enabled="true"/>
<menuitem type="seperator" enabled="false"/>
<menuitem label="Exit" enabled="true"/>
</menuitem>
<menuitem label="View" accesskey="v">
<menuitem label="Zoom In" enabled="true" maxValue="200"/>
<menuitem label="Zoom Out" enabled="true" maxValue="25"/>
</menuitem>
<menuitem label="Tools" enabled="false">
<menuitem label="item1" enabled="false"/>
<menuitem label="item2" enabled="false"/>
</menuitem>
</root>
Thanks in advance :)
Sriss
To open a menu at its position :
var fileMenu:Menu = mnuBar.getMenuAt(0);
pnt : Point = new Point(mnuBar.x,mnuBar.y + mnuBar.height);
pnt = localToGlobal(pnt);
fileMenu.show(pnt.x,pnt.y);
I solved it :)
var fileMenu:Menu = myMenuBar.getMenuAt(0);
fileMenu.show();
It works, but now the problem is .. the submenu is popping out at (0,0) location of the application not at the file Menu item ..!!!
It's a fake Menu!
A better way to do it is:
e.preventDefault(); // will play against pressed ESC, too
menuBar.setFocus();
menuBar.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, false, false, 0, Keyboard.RIGHT));

Resources