How to change contextMenu default mouse button? - javafx

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.

Related

JavaFX: Why that does not connect between label and textfield with setLabelFor?

I have the following code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setLabelFor(vSuchenTextfield);
when I click on the vLblSuchfeld label with the mouse,
the text field vSuchenTextfield is not activated. What am I doing wrong?
If you want to bring focus to the textfield when you click on the label, I suggest you implement the following (untested) code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setOnMouseClicked(event -> {
vSuchenTextfield.requestFocus();
});

Show text on TextField after button is pressed (JavaFX)

I'm trying to build a calculator, and I have looked all over internet and the examples don't help me, so I have buttons created and everything, I'm trying to display on:
TextField Result = new TextField();
Result.setEditable(false);
Result.setAlignment(Pos.CENTER_RIGHT);
Result.setMinSize(210, 30);
Result.textProperty().bind(Bindings.format("%.0f" , value));
pane.getChildren().add(Result);
the number of the button pressed, how do I do that? Let's say the button was:
Button uno = new Button("1");
uno.setMinSize(40, 40);
pane.getChildren().add(uno);
How do I make the text field Result show number 1?
BIG Thanks!
The simplest way is to add a listener for each your button and change your TextField dynamically:
uno.pressedProperty().addListener((o, old, newValue) -> Result.setText("1"));
dos.pressedProperty().addListener((o, old, newValue) -> Result.setText("2"));
Note, when the user pressed and unpressed a button your listener will react twice with newValue = true/false. Do the additional checking if needed:
uno.pressedProperty().addListener((o, old, newValue) -> {
if (newValue) {
Result.setText("1")
}
});
UPDATE:
Don't forget to remove this line as wrong solution:
Result.textProperty().bind(Bindings.format("%.0f" , value));

how to have a running program add tab panes javafx

Hello this is my question, In order to let a user add tabs with a new button would i need to create a whole new border panes and tab panes for this to work?
No you don't need if you have already created tabPane previously.
Button btn = new Button("add new tab");
btn.setOnAction( (e) -> {
tabPane.getTabs.add(new Tab("new tab"), new VBox(new Label(Content)) );
});

ContextMenu and programmatically selecting an item

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

Call ChoiceBox image click

I want to create ChoiceBox which I want to call when I press the image below. Is there any to call ChoiceBox menu when I click on the image?
ChoiceBox cb = new ChoiceBox();
cb.setItems(FXCollections.observableArrayList(
"New Document", "Open ",
new Separator(), "Save", "Save as")
);
You can initially put the ChoiceBox in your pane and set the visibility as false
cb.setVisible(false);
Later, when you click on the image, you can set the visibility as true !
image.setOnAction(new EventHandler<>{
put void onAction()
{
cb.setVisible(true);
}
});
Note : I just typed the code, so not sure whether it will compile or not ! Just wanted to give you an idea !

Resources