ContextMenu and programmatically selecting an item - javafx

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));

Related

How to change contextMenu default mouse button?

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.

Proper way to add nodes using loop

I'm trying to make a small application that displays the contents of an arrayList but I have not been succesful. Currently I have a loop concatenating each object in the list with their toString method. This is not the solution I want for displaying the arrayList however. I want to be able to add a separate label and button for each object in the list so that I can press the button and open a window to change the item's data. Is there a way to add multiple javafx nodes using a loop AND add a parameter/id to a button? I know you can set an id to a button using fxml but I have to make this application without using fxml and have not been able to figure out how to do it otherwise.
Here is a code example of how I add the text right now
String list;
for (Registration registration : registrationList) {
list += registration.toString();
}
label.setText(list);
and what I've tried so far
for (Registration registration : registrationList) {
Label dynamicLabel = new Label(registration.toString());
Button dynamicButton = new Button("" + registration.getId());
layout1.getChildren().addAll(dynamicLabel, dynamicButton);
}
please let me know if something is unclear in my question, thanks in advance.
The problem is that you're thinking about the controls like data, and you need to treat them like regular objects. You're also thinking about the entire thing monolithically, which makes it difficult to see a solution.
Think about a Label + a Button as a distinct unit that works together. Define them together and you can freely have them talk to each other without figuring out ways to identify them and link them together later on.
Something like this will do what you're looking for:
public class ButtonActionSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
List<String> strings = List.of("String 1", "String 2", "String 3");
VBox vBox = new VBox(5);
vBox.getChildren().addAll(strings.stream().map(string -> createHBox(string)).collect(Collectors.toList()));
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
private Node createHBox(String string) {
Label label = new Label(string);
Button button = new Button("Change Text");
button.setOnAction(evt -> new TextInputDialog(label.getText()).showAndWait().ifPresent(label::setText));
return new HBox(5, label, button);
}
}
When you do it like this, you don't need to hang on to references to any of the screen elements at all; just create them, set the action on the Button and stick them on the screen. They just work after that.

JavaFx add context menu from controller

I am trying my way through JavaFX and still have many - probably silly - beginner questions.
My problem of the day is the following:
I am creating, in Scene builder and Controller, a FlowPane to which I want to add a right-click option, that opens a Context Menu.
Through the scene builder I have added the function OnContextMenuRequested and defined it in the Controller.
To check, I have added a print commend and a Dialog Box to the function, which work well.
Yet, the Context Menu does not work..
Anybody could help and tell me what am I missing???
Thanks in advance...
public void contextMenu(ContextMenuEvent contextMenuEvent) {
// working fine ..
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Look");
alert.setContentText("Message");
alert.showAndWait();
// working fine
System.out.println("Hello");
// Context Menu ......... not working
ContextMenu contextMenu = new ContextMenu();
MenuItem quit = new MenuItem("quit");
MenuItem hello = new MenuItem("hello");
contextMenu.getItems().addAll(quit, hello);
contextMenu.setX(10.0);
contextMenu.setY(10.0);
contextMenu.show();
????.setContextMenu(????)
}
Unless you have a control, you need to show the ContextMenu "manually" using one of the methods defined in ContextMenu:
// contextMenu.setX(10.0);
// contextMenu.setY(10.0);
contextMenu.show((Node) contextMenuEvent.getSource(), contextMenuEvent.getScreenX(), contextMenuEvent.getScreenY());

javafx TitledPane click event only for Title

Is there any way I can have an event that only triggers if I click the Title of a TitledPane?
I have several Nodes in a Graph Editor and currently they are draggable.
But I want them only to drag when i drag the Title not if I click anywhere on the pane.
the mouseClick event seems not to work for me.
Does anyone have suggestions?
Don't set the text on the titled pane, but instead create a label and set it as the graphic for the titled pane. Then you can register a mouse handler with the label:
private TitledPane createClickableTitledPane(String text) {
Label label = new Label(text);
label.setOnMouseClicked(e -> System.out.println("Click on "+text));
TitledPane titledPane = new TitledPane();
titledPane.setGraphic(label);
return titledPane ;
}
StackPane titleRegion = (StackPane) titledPane.lookup(".title");
titleRegion.setOnMouseClicked(System.out::println);
EDIT:
Sometimes titledPane.lookup(".title") returns null which means CSS is not applied to the node. To resolve this issue, you need to use applyCss() and layout() on the pane that contains the TitledPane.
See:
JavaFX TitledPane lookup(.title) returns null

JavaFX TreeView: remove expand / collapse button (disclosure node) & functionality

I want to have a TreeView that has all of its children permanently expanded, and I don't want the user to be able to expand or collapse any of the children.
To do this I've found that I need to do the following:
Remove icon with CSS (Done)
Change expand and collapse image TreeView JavaFX 2.2
[edit] Above link should be used to change image; to remove completely, use this solution: https://stackoverflow.com/a/27831191/4430591
Remove double click functionality (Done)
Disable TreeItem's default expand/collapse on double click JavaFX 2.2
[edit] Remove ability to collapse / expand using keyboard arrrow keys (Done)
Given in José Pereda's solution below ( https://stackoverflow.com/a/27831085/4430591 )
[edit] Remove ability to right click for a ContextMenu (Done)
Given in José Pereda's solution below ( https://stackoverflow.com/a/27831085/4430591 )
Remove icon's clickablity (How do I do this?)
[edit] solution: https://stackoverflow.com/a/27831191/4430591
Even though the icon is no longer visible, it's still clickable. I don't see any way of filtering this; I only see ways to be able to respond to it after the fact.
Also, if I'm missing anything else that I need to do to ensure this functionality, please let me know.
I feel quite silly. I think this was mostly just a matter of not knowing what that darn arrow was called. Apparently it's a disclosureNode? Maybe that's common knowledge.
In the custom defined TreeCell, all I did was add this line in the updateItem method:
setDisclosureNode(null);
The solution to avoid modifying the skin or the default behavior is more simple if we trap the clicks before they are dispatched, and consume the right ones.
For that we can use an EventDispatcher, to filter both the mouse pressed and the right click over the arrows, which are StackPane nodes:
class CellEventDispatcher implements EventDispatcher {
private final EventDispatcher original;
public CellEventDispatcher(EventDispatcher original) {
this.original = original;
}
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED) ||
event.getEventType().equals(ContextMenuEvent.ANY)){
event.consume();
}
if(event instanceof KeyEvent && event.getEventType().equals(KeyEvent.KEY_PRESSED)){
if((((KeyEvent)event).getCode().equals(KeyCode.LEFT) ||
((KeyEvent)event).getCode().equals(KeyCode.RIGHT))){
event.consume();
}
}
return original.dispatchEvent(event, tail);
}
}
Now we apply our custom dispatcher to the tree view:
#Override
public void start(Stage primaryStage) {
TreeView<String> tree = new TreeView<>();
...
EventDispatcher treeOriginal = tree.getEventDispatcher();
tree.setEventDispatcher(new CellEventDispatcher(treeOriginal));
Scene scene = new Scene(tree);
primaryStage.setScene(scene);
primaryStage.show();
}
This will consume any click (left or right) over the arrows on the tree.
EDIT
Added to the event dispatcher class the case where the user uses the keyboard to traverse the tree view, consuming the collapse/expand events with arrow LEFT or RIGHT.

Resources