I have a TreeView with a context menu, all works except when tree is empty. When tree is empty, I want to prevent show context menu to user.
//Set up context menu and menu items
final ContextMenu contextMenu = new ContextMenu();
final MenuItem miSubir = new MenuItem("Subir");
final MenuItem miBajar = new MenuItem("Bajar");
final MenuItem miBorrar = new MenuItem("Borrar");
//add events from clic on menu items
miBorrar.setOnAction((ActionEvent event) -> {
...
});
...
//Add menu items to context menu
contextMenu.getItems().add(miSubir);
contextMenu.getItems().add(miBajar);
contextMenu.getItems().add(miBorrar);
//Associate context menu to treeview
treeEjercicios.setContextMenu(contextMenu);
When I use Table component, I fix the problem with:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(contextMenu)
);
But I don't know how to apply to use with TreeView or any other alternative?
If the root item is shown, i.e. tree.setShowRoot(true), it can be supposed that when the root item is null the treeview is empty. So we can bind it
tree.contextMenuProperty().bind(
Bindings.when( Bindings.isNull( tree.rootProperty() ) )
.then( (ContextMenu) null)
.otherwise( contextMenu )
);
Else if the root item is not show, then the tree can be supposed to be empty if this root item has no children, i.e. when isLeaf() returns true. In this case the binding will be:
tree.contextMenuProperty().bind(
Bindings.when( tree.getRoot().leafProperty() )
.then( ( ContextMenu ) null )
.otherwise( contextMenu )
);
Related
I have a contextMenu and I added it to a button. I want the contextMenu to be displayed when I left-click(PRIMARY) on the button. How can I do this? Because by default just right-clicking does this.
I tried this way but it did not work
Button sortBy = new Button();
ContextMenu sortByMenu = new ContextMenu();
sortByMenu.addEventFilter(MouseEvent.MOUSE_PRESSED, ev -> {
if (ev.getButton() == MouseButton.PRIMARY) {
//does not do anything
}
});
sortBy.setContextMenu(sortByMenu);
MenuButton
MenuButton is a button which, when clicked or pressed, will show a ContextMenu.
Example code from the javadoc:
MenuButton m = new MenuButton(
"Eats"
);
m.getItems().addAll(
new MenuItem("Burger"),
new MenuItem("Hot Dog")
);
ChoiceBox
You could also consider a ChoiceBox, depending on what you are trying to do.
The ChoiceBox is used for presenting the user with a relatively small set of predefined choices from which they may choose.
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll(
"item1",
"item2",
"item3"
);
To select a sortBy sorting field from a list of choices, a ChoiceBox would probably be a good fit.
I have siteMapNode inside SiteMap in Web.sitemap and want to hide one of the menu items, when I am at the page .../example.aspx?title=hiding
I tried to hide that menu from example.aspx.cs at Page_Load with smth like:
MenuItemCollection menuItems = Menu.Items;
MenuItem menuItem = new MenuItem();
foreach (MenuItem item in menuItems)
{
if (item.Text == "home")
menuItem = item;
}
menuItems.Remove(menuItem);
but I couldn't find needed item. Also I have tried to add logic into MenuTop_MenuItemDataBound, but it isn't executing at every url change
There does not seem to be API for programmatically "selecting" ContextMenu items? By selecting I mean the equivalent of tapping up and down keys (or hovering the mouse over an item). I really only need to select the first item, when a ContextMenu is shown. I attempted to fire a down keyevent upon showing the menu, but nothing happened.. perhaps I constructed the event wrongly.
To get this working, we could use some private API. ContextMenu skin (ContextMenuSkin) uses a ContextMenuContent object, as a container with all the items.
We just need to request the focus for the first of these items.
But for this we could just use some lookups to find the first menu-item CSS selector. This has to be done after the stage has been shown.
This example will show a context menu with focus on the first item:
#Override
public void start(Stage primaryStage) {
MenuItem cmItem1 = new MenuItem("Item 1");
cmItem1.setOnAction(e->System.out.println("Item 1"));
MenuItem cmItem2 = new MenuItem("Item 2");
cmItem2.setOnAction(e->System.out.println("Item 2"));
final ContextMenu cm = new ContextMenu(cmItem1,cmItem2);
Scene scene = new Scene(new StackPane(), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
scene.setOnMouseClicked(t -> {
if(t.getButton()==MouseButton.SECONDARY){
cm.show(scene.getWindow(),t.getScreenX(),t.getScreenY());
// Request focus on first item
cm.getSkin().getNode().lookup(".menu-item").requestFocus();
}
});
}
For me solution provided in accepted answer didn't work correctly as item was only highlighted but not really selected (<Enter> was not accepting a value).
Instead of that constructing a proper KeyEvent did the work except a bug that only after first letter popup was working correctly.
Finally I combined both and got what I'd wanted:
// 'this' is related to parent component of ContextMenu
popup.show(this, x, y);
// Request focus on first item (sort of hack)
popup.getSkin().getNode().lookup(".menu-item").requestFocus();
this.fireEvent(new KeyEvent(
KeyEvent.KEY_PRESSED, "", "",
KeyCode.DOWN, false, false, false, false));
How can I add custom context menu items to the context menu of a spark TextInput component? I am using Flex 4.5.1.
Assuming I have a spark TextInput called sparktext:
trace(sparktext.contextMenu) // returns null
trace(sparktext.textDisplay.contextMenu) // returns null
Since those returned null, I though I would just create a contextMenu using:
sparktext.contextMenu = new ContextMenu()
//add entries
Or
sparktext.textDisplay.contextMenu = new ContextMenu()
//add entries
Now a contextMenu is created. But the created context menu is a "flash" context menu, displaying items like "play", "zoom" and so on. I have lost entries for items like "copy", "paste" and so on.
I have tried creating a RichEditableText, and its contextMenu property actually contains a ContextMenu object. I am at lost as to why the RichEditableText object, which is stored in TextInput.textDisplay does not contain a ContextMenu object in its contextMenu property.
Can anyone shed some light on this and how I can ADD context menu items to the context menu of a TextInput?
Still couldn't work out why sparktext.textDisplay.contextMenu is NULL.
But here's what I did:
var testmenu:ContextMenu = new ContextMenu();
testmenu.hideBuiltInItems();
testmenu.clipboardMenu = true; //Use this to show the options such as copy, paste and so on.
sparktext.textDisplay.contextMenu = testmenu;
var contextMenu:ContextMenu = new ContextMenu();
contextMenu.hideBuiltInItems();
var contactList : ContextMenuItem = new ContextMenuItem("Add to Existing List");
contactList.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doStaticListCommand);
var newContactList : ContextMenuItem = new ContextMenuItem("Add a New List");
newContactList.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doNewStaticListCommand);
var removeContactList : ContextMenuItem = new ContextMenuItem("Remove contact from List");
removeContactList.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doRemoveListCommand);
var deletecontact:ContextMenuItem = new ContextMenuItem("Delete contact");
deletecontact.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, dodeleteconactCommand);
var TimeList : ContextMenuItem = new ContextMenuItem("Add Time Spent");
TimeList.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doTimeListCommand);
contextMenu.customItems.push(contactList);
contextMenu.customItems.push(newContactList);
contextMenu.customItems.push(deletecontact);
contextMenu.customItems.push(removeContactList);
In flex i done contex menu , if i rigt click then show context menu item but i want to hidden particular context menu item in list , is it possiable hidden and show particular items in context menu ? please refer me , i tried key value based
if(Application.application.contact_key==1)
{
contextMenu.customItems.push(deletecontact);
}
else
{
contextMenu.customItems.push(removeContactList);
}
contextMenu.customItems.push(TimeList);
return contextMenu;
Within itemRenderer
All coding on contactListItemRenderer.as and call to datagrid like
<mx:DataGridColumn itemRenderer="com.view.Contact.ContactListItemRenderer"
dataField="fullName" headerText="Full Name" />
You can access the custom menu items by contextMenu.customItem and hide/show any particular item. For the build in menu items you can access contextMenu.builtInItems (see the code below):
contextMenuCustom.builtInItems.zoom = false;
contextMenuCustom.builtInItems.save = true;