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









share|improve this question






















  • 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






  • 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















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









share|improve this question






















  • 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






  • 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













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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 at 17:45









Venkata Raju

1,8281627




1,8281627












  • 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






  • 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


















  • 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






  • 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
















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












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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































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





    share|improve this answer



























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





      share|improve this answer

























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





        share|improve this answer














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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 5 at 19:08

























        answered Nov 5 at 18:49









        JKostikiadis

        2,00721121




        2,00721121






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings