Can I get key pressed event setOnKeyPress on a popup?
My parent root is opening one popup box and this popup box contains "ok" button.
I want to generate an event on pressing the enter key on keyboard and it should "submit" (trigger "ok" button of) that popup dialog box.
I have used the following code:
NicheSuitePOSController.getApplication().getScene().getRoot().getScene().getRoot().getScene()
.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
System.out.println("$$$$$$$$ 131 enter pressed" + ke.getCode());
if (ke.getCode().equals(KeyCode.ENTER)) {
System.out.println("$$$$$$$$ 133 enter pressed");
}
}
});
Try to use
Button.defaultButtonProperty() without attaching any key event handlers to the scene or dialogbox.
Related
I want to bind actions of JavaFX buttons to keyboard keys.
I want the following functionality:
When I click 1, the action for "Select one file" should trigger
When I click 9, the action for "Select multi file" should trigger
When I click Enter, the action for "OK" button should trigger
When I click Esc, the action for "Cancel" button should trigger
Todo this you could use EventFilters
If you want to have have it triggered only when one key is pressed:
addEventFilter(KeyEvent.KEY_PRESSED, event ->
{
if(event.getCode().equals(KeyCode.DIGIT1))
{
System.out.println("1 Pressed");
//Then either call the method directly
selectOneFile();
//Or fire the button
selectOneFileBtn.fire();
}
});
But as #ItachiUchiha (and I) recommend, you should use a combination of keys:
addEventFilter(KeyEvent.KEY_PRESSED, event ->
{
if(event.isAltDown() && event.getCode().equals(KeyCode.DIGIT1))
{
System.out.println("Alt + 1 Pressed");
//Then again, either call the method directly
selectOneFile();
//Or fire the button
selectOneFileBtn.fire();
}
});
I'm using c# wpf. What i want to do is,
A button: When the user clicks the button, rectangular selection is enabled. Then user can draw rectangle on panel.
Mouse down event: When the user clicks the mouse button , it gets the x axis of the current position on panel.
Mouse move event: After clickling , if the user moves the mouse to the left my code1 is going to run. If the user moves the mouse to the right my code2 is going to run.
Code1:
When the user moves the mouse to the right after clicking on panel user is going to draw green rectangle and when user releases the mouse button that rectangle disappears and selects the objects in the rectangle.
Code2:
When the user moves the mouse to the left after clicking on panel user is going to draw red rectangle and when user relases the mouse button the rectangle disappears and selecte the objects in the rectangle and the objects that rectangle intersects.
Here is what i got:
public static System.Drawing.Point mouseLocation2;
public static System.Drawing.Point posmousedoWN;
Button Click Event:
private void btnSelectBox_OnClick(object sender, RoutedEventArgs e)
{
if (btnSelectBox.IsChecked.Value)
{
if (mypartialclass.mouseLocation2.X > mypartialclass.posmousedoWN.X)
{
//code1
}
if (mypartialclass.mouseLocation2.X < mypartialclass.posmousedoWN.X)
{
//code2
}
}
}
Mouse Down event: (This event is in mypartial class)
protected override void OnMouseDown(MouseButtonEventArgs e)
{
posmousedoWN = RenderContextUtility.ConvertPoint(e.GetPosition(this));
}
Mouse Move event: (This event is in mypartial class)
protected override void OnMouseMove(MouseEventArgs e)
{
mouseLocation2 = RenderContextUtility.ConvertPoint(e.GetPosition(this));
}
This code is not working. When i click the button first code1 runs. Then always code2 runs. What am i missing?
Why does the JavaFX alert dialog fires the Platform.exit(); when I press the Enter key even though the focused button in the alert dialog is Cancel?
soaStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirm");
alert.setHeaderText("Are you sure you want to exit?");
alert.setContentText("Press OK to exit, or Cancel to stay.");
alert.initOwner(soaStage);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK)
{
Platform.exit();
}
else
{
event.consume();
}
}
});
Default buttons are fired on enter
The OK button is fired when you press enter because it is a default button.
A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.
In the default JavaFX 8 Modena stylesheet, the default button is indicated by being a blue color rather than than the gray color of a standard button.
How to remove default button behaviour
You can remove this behavior from the alert dialog by not making the OK button a default button:
Button okButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
okButton.setDefaultButton(false);
I advise you not to do this, but instead to always leave a default button in alert dialogs.
On OS X, standard alert type dialogs have a default button which is triggered by enter even if another button is focused, so the standard behavior in JavaFX is consistent with that. Note: to allow this behavior in default dialogs in OS X it is necessary to enable full keyboard access.
If you do change the OK button to not be a default button, I suggest you change its text to something else (e.g. Exit for your case):
okButton.setText("Exit");
How to make enter fire focused buttons
Now, if you also want to make it so that the focused button fires when you press enter, then you can do this:
EventHandler<KeyEvent> fireOnEnter = event -> {
if (KeyCode.ENTER.equals(event.getCode())
&& event.getTarget() instanceof Button) {
((Button) event.getTarget()).fire();
}
};
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getButtonTypes().stream()
.map(dialogPane::lookupButton)
.forEach(button ->
button.addEventHandler(
KeyEvent.KEY_PRESSED,
fireOnEnter
)
);
Note: In any case, focused buttons are always fired when you press space.
We can add ENTER binding to the whole buttons by creating a class that need to be instantiated once when the application starts.
public class EnableButtonEnterKey extends ButtonBehavior<Button> {
public EnableButtonEnterKey() {
super(new Button());
BUTTON_BINDINGS.add(new KeyBinding(ENTER, KEY_PRESSED, "Press"));
BUTTON_BINDINGS.add(new KeyBinding(ENTER, KEY_RELEASED, "Release"));
}
}
When starting the application, call
new EnableButtonEnterKey();
That's it.
I am using scenebuilder to make the UI. I want to make the color of the buttons change when mouse is pressed or when the button is touched. Can I set the same method for both mouse pressed and screen touched events and also set the same events for multiple buttons? Like there are 3 buttons and I want to change their color in mouse pressed and screen touched events and use only one method for all.
Thank you
Lets say, you have three buttons
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Create a method saying
private void handleButtonAction(ActionEvent event) {
// Button was clicked, change color
((Button)event.getTarget).setStyle("-fx-background-color:PINK");
}
All buttons have a setOnAction() which is fired on both mouse pressed and screen touched events.
JavaDoc says
The button's action, which is invoked whenever the button is fired.
This may be due to the user clicking on the button with the mouse, or
by a touch event, or by a key press, or if the developer
programmatically invokes the fire() method.
Use it :
button1.setOnAction(this::handleButtonAction);
button2.setOnAction(this::handleButtonAction);
button3.setOnAction(this::handleButtonAction);
If you are using FXML
You can define one action for all buttons :
<Button id="button1" onAction="#handleButtonAction"/>
<Button id="button2" onAction="#handleButtonAction"/>
<Button id="button3" onAction="#handleButtonAction"/>
Inside the controller :
#FXML
private void handleButtonAction(ActionEvent event) {
// Button was clicked, change color
((Button)event.getTarget).setStyle("-fx-background-color:PINK");
}
I have two buttons viz. save and update. On both buttons, I am opening pop-up window using Model Pop-up extender. The Pop-up has "ok" as close button. Now what I want is when user clicks on the "Ok" button in the pop-up, it should call the OnClick_save() or OnClick_update() depends on the button who called pop-up.
That means, when user clicks on save button, it will open pop-up window. After entering password in the pop-up window when user click on the Ok button, it should go to the OnClick_save() event. Same thing should happen for update button. How can I do so...???
You can use hidden controls for pop up's OK and Cancel button, and show/hide modal popup programatically. before/after doing so you can call your desired methods or whatever you want to do.
You can do this using event handler..
public delegate void MyEventHandler(object sender, EventArgs e);
MyEventHandler handler = MyEvent;
if(handler != null)
{
handler(this, e);
}
or
this.button1.Click += new System.EventHandler(this.button1_Click);
Or
Write a common method which can invoke from save_click event or ok_click
If you find it useful, please mark it as your answer else let me know...