Stop file chooser key event propagation to the (parent) stage in JavaFX
up vote
3
down vote
favorite
I need to listen to key events in stage but not when the FileChooser is displayed.
Is there a way to stop KeyEvent (e.g. Esc key is pressed.) propagation to (parent) Stage, when FileChooser is displayed?
OR
Detect that the KeyEvent is propagated from FileChooser?
class KeyEventTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae ->
{
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.setTitle("Test");
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent ->
{
// Do not trigger when Esc key is pressed on FileChooser
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
});
stage.centerOnScreen();
stage.show();
}
public static void main(String args)
{
launch(args);
}
}
javafx
add a comment |
up vote
3
down vote
favorite
I need to listen to key events in stage but not when the FileChooser is displayed.
Is there a way to stop KeyEvent (e.g. Esc key is pressed.) propagation to (parent) Stage, when FileChooser is displayed?
OR
Detect that the KeyEvent is propagated from FileChooser?
class KeyEventTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae ->
{
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.setTitle("Test");
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent ->
{
// Do not trigger when Esc key is pressed on FileChooser
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
});
stage.centerOnScreen();
stage.show();
}
public static void main(String args)
{
launch(args);
}
}
javafx
I had thought we could simply remove theEventFilterwhen showing theFileChooserand re-add it when that closes, but in my tests, it still passes theKeyEventto the parent stage and I do not know why.
– Zephyr
Nov 5 at 18:41
This does not seem to be possible with thejavafx.stage.FileChooserclass, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even usekeyEvent.getSource()to determine that the key was pressed whileFileChooserwas focused.
– Zephyr
Nov 5 at 18:45
1
The problem seems to be theFileChooseris closed whenESCis pressed. By the time theESCkey is released theFileChooserhas closed and theStagehas regained focus. This also means that theFileChooseris not the source of the event, but rather whateverNodehas focus when theStagebecomes focused.
– Slaw
Nov 5 at 19:20
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I need to listen to key events in stage but not when the FileChooser is displayed.
Is there a way to stop KeyEvent (e.g. Esc key is pressed.) propagation to (parent) Stage, when FileChooser is displayed?
OR
Detect that the KeyEvent is propagated from FileChooser?
class KeyEventTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae ->
{
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.setTitle("Test");
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent ->
{
// Do not trigger when Esc key is pressed on FileChooser
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
});
stage.centerOnScreen();
stage.show();
}
public static void main(String args)
{
launch(args);
}
}
javafx
I need to listen to key events in stage but not when the FileChooser is displayed.
Is there a way to stop KeyEvent (e.g. Esc key is pressed.) propagation to (parent) Stage, when FileChooser is displayed?
OR
Detect that the KeyEvent is propagated from FileChooser?
class KeyEventTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae ->
{
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.setTitle("Test");
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent ->
{
// Do not trigger when Esc key is pressed on FileChooser
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
});
stage.centerOnScreen();
stage.show();
}
public static void main(String args)
{
launch(args);
}
}
javafx
javafx
asked Nov 5 at 17:45
Venkata Raju
1,8281627
1,8281627
I had thought we could simply remove theEventFilterwhen showing theFileChooserand re-add it when that closes, but in my tests, it still passes theKeyEventto the parent stage and I do not know why.
– Zephyr
Nov 5 at 18:41
This does not seem to be possible with thejavafx.stage.FileChooserclass, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even usekeyEvent.getSource()to determine that the key was pressed whileFileChooserwas focused.
– Zephyr
Nov 5 at 18:45
1
The problem seems to be theFileChooseris closed whenESCis pressed. By the time theESCkey is released theFileChooserhas closed and theStagehas regained focus. This also means that theFileChooseris not the source of the event, but rather whateverNodehas focus when theStagebecomes focused.
– Slaw
Nov 5 at 19:20
add a comment |
I had thought we could simply remove theEventFilterwhen showing theFileChooserand re-add it when that closes, but in my tests, it still passes theKeyEventto the parent stage and I do not know why.
– Zephyr
Nov 5 at 18:41
This does not seem to be possible with thejavafx.stage.FileChooserclass, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even usekeyEvent.getSource()to determine that the key was pressed whileFileChooserwas focused.
– Zephyr
Nov 5 at 18:45
1
The problem seems to be theFileChooseris closed whenESCis pressed. By the time theESCkey is released theFileChooserhas closed and theStagehas regained focus. This also means that theFileChooseris not the source of the event, but rather whateverNodehas focus when theStagebecomes focused.
– Slaw
Nov 5 at 19:20
I had thought we could simply remove the
EventFilter when showing the FileChooser and re-add it when that closes, but in my tests, it still passes the KeyEvent to the parent stage and I do not know why.– Zephyr
Nov 5 at 18:41
I had thought we could simply remove the
EventFilter when showing the FileChooser and re-add it when that closes, but in my tests, it still passes the KeyEvent to the parent stage and I do not know why.– Zephyr
Nov 5 at 18:41
This does not seem to be possible with the
javafx.stage.FileChooser class, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even use keyEvent.getSource() to determine that the key was pressed while FileChooser was focused.– Zephyr
Nov 5 at 18:45
This does not seem to be possible with the
javafx.stage.FileChooser class, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even use keyEvent.getSource() to determine that the key was pressed while FileChooser was focused.– Zephyr
Nov 5 at 18:45
1
1
The problem seems to be the
FileChooser is closed when ESC is pressed. By the time the ESC key is released the FileChooser has closed and the Stage has regained focus. This also means that the FileChooser is not the source of the event, but rather whatever Node has focus when the Stage becomes focused.– Slaw
Nov 5 at 19:20
The problem seems to be the
FileChooser is closed when ESC is pressed. By the time the ESC key is released the FileChooser has closed and the Stage has regained focus. This also means that the FileChooser is not the source of the event, but rather whatever Node has focus when the Stage becomes focused.– Slaw
Nov 5 at 19:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
It is possible to achieve it. You are correct that if you close the FileChooser using either escape or ALT+F4 the event will be propagated, but you need to think which event will? if you have a look only the KeyEvent.KEY_RELEASED will. So if you just add an event filter recording when the key was pressed you can find out when you pressed the escape ( or alt+f4 ) in your main stage or in your FileChooser
So more or less you are trying to find out if the KEY_PRESSED was handled by FileChooser or not. If it was, then the KEY_RELEASED will also be handled from FileChooser otherwise we trigger the KeyEvent in our main app.
Here is an example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private boolean captureKeys = false;
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae -> {
captureKeys = false;
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
captureKeys = true;
// For debug
// KeyCode keyCode = keyEvent.getCode();
// System.out.println("KEY_PRESSED on Stage : " + keyCode);
});
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if(captureKeys) {
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
}
});
stage.centerOnScreen();
stage.show();
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
It is possible to achieve it. You are correct that if you close the FileChooser using either escape or ALT+F4 the event will be propagated, but you need to think which event will? if you have a look only the KeyEvent.KEY_RELEASED will. So if you just add an event filter recording when the key was pressed you can find out when you pressed the escape ( or alt+f4 ) in your main stage or in your FileChooser
So more or less you are trying to find out if the KEY_PRESSED was handled by FileChooser or not. If it was, then the KEY_RELEASED will also be handled from FileChooser otherwise we trigger the KeyEvent in our main app.
Here is an example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private boolean captureKeys = false;
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae -> {
captureKeys = false;
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
captureKeys = true;
// For debug
// KeyCode keyCode = keyEvent.getCode();
// System.out.println("KEY_PRESSED on Stage : " + keyCode);
});
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if(captureKeys) {
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
}
});
stage.centerOnScreen();
stage.show();
}
}
add a comment |
up vote
0
down vote
accepted
It is possible to achieve it. You are correct that if you close the FileChooser using either escape or ALT+F4 the event will be propagated, but you need to think which event will? if you have a look only the KeyEvent.KEY_RELEASED will. So if you just add an event filter recording when the key was pressed you can find out when you pressed the escape ( or alt+f4 ) in your main stage or in your FileChooser
So more or less you are trying to find out if the KEY_PRESSED was handled by FileChooser or not. If it was, then the KEY_RELEASED will also be handled from FileChooser otherwise we trigger the KeyEvent in our main app.
Here is an example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private boolean captureKeys = false;
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae -> {
captureKeys = false;
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
captureKeys = true;
// For debug
// KeyCode keyCode = keyEvent.getCode();
// System.out.println("KEY_PRESSED on Stage : " + keyCode);
});
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if(captureKeys) {
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
}
});
stage.centerOnScreen();
stage.show();
}
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
It is possible to achieve it. You are correct that if you close the FileChooser using either escape or ALT+F4 the event will be propagated, but you need to think which event will? if you have a look only the KeyEvent.KEY_RELEASED will. So if you just add an event filter recording when the key was pressed you can find out when you pressed the escape ( or alt+f4 ) in your main stage or in your FileChooser
So more or less you are trying to find out if the KEY_PRESSED was handled by FileChooser or not. If it was, then the KEY_RELEASED will also be handled from FileChooser otherwise we trigger the KeyEvent in our main app.
Here is an example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private boolean captureKeys = false;
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae -> {
captureKeys = false;
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
captureKeys = true;
// For debug
// KeyCode keyCode = keyEvent.getCode();
// System.out.println("KEY_PRESSED on Stage : " + keyCode);
});
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if(captureKeys) {
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
}
});
stage.centerOnScreen();
stage.show();
}
}
It is possible to achieve it. You are correct that if you close the FileChooser using either escape or ALT+F4 the event will be propagated, but you need to think which event will? if you have a look only the KeyEvent.KEY_RELEASED will. So if you just add an event filter recording when the key was pressed you can find out when you pressed the escape ( or alt+f4 ) in your main stage or in your FileChooser
So more or less you are trying to find out if the KEY_PRESSED was handled by FileChooser or not. If it was, then the KEY_RELEASED will also be handled from FileChooser otherwise we trigger the KeyEvent in our main app.
Here is an example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
private boolean captureKeys = false;
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) {
Button browseBtn = new Button("Browse File System");
browseBtn.setOnAction(ae -> {
captureKeys = false;
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenMultipleDialog(stage);
});
stage.setScene(new Scene(new BorderPane(browseBtn), 500, 200));
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
captureKeys = true;
// For debug
// KeyCode keyCode = keyEvent.getCode();
// System.out.println("KEY_PRESSED on Stage : " + keyCode);
});
stage.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if(captureKeys) {
KeyCode keyCode = keyEvent.getCode();
System.out.println(keyCode);
}
});
stage.centerOnScreen();
stage.show();
}
}
edited Nov 5 at 19:08
answered Nov 5 at 18:49
JKostikiadis
2,00721121
2,00721121
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159546%2fstop-file-chooser-key-event-propagation-to-the-parent-stage-in-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I had thought we could simply remove the
EventFilterwhen showing theFileChooserand re-add it when that closes, but in my tests, it still passes theKeyEventto the parent stage and I do not know why.– Zephyr
Nov 5 at 18:41
This does not seem to be possible with the
javafx.stage.FileChooserclass, as it is not customizable. You may need to implement a custom file chooser instead. We cannot even usekeyEvent.getSource()to determine that the key was pressed whileFileChooserwas focused.– Zephyr
Nov 5 at 18:45
1
The problem seems to be the
FileChooseris closed whenESCis pressed. By the time theESCkey is released theFileChooserhas closed and theStagehas regained focus. This also means that theFileChooseris not the source of the event, but rather whateverNodehas focus when theStagebecomes focused.– Slaw
Nov 5 at 19:20