6 Replies Latest reply: Nov 22, 2012 1:25 PM by 935521 RSS

    Event on Del key keyboard

    935521
      As from James_D code posted here

      How to progate an event to a children using JavaFx2

      I am interested in how to attach an event to group made of Line and Anchor, or at line only in order to intercept the delete key keyboard press to remove the whole group.

      Thanks
        • 1. Re: Event on Del key keyboard
          famedoro
          Hi,
          have you seen http://docs.oracle.com/javafx/2/events/handlers.htm#BABBHHCJ ?
          • 2. Re: Event on Del key keyboard
            935521
            Hi,

            yes I am aware of this and I have tried it but I can't add the event to the group.

            The example does not work when I try to add the event to the group created with

            group.getChildren().addAll(line, startAnchor, endAnchor);
            • 3. Re: Event on Del key keyboard
              James_D
              Keyboard events are only "heard" by the Node that has keyboard focus - so you would need some way for your Group/Line to have keyboard focus. Try registering a mouse handler with the line which requests keyboard focus when the user clicks on the line.
              • 4. Re: Event on Del key keyboard
                935521
                Hi James D,

                thanks for your reply: do you mean something like this?
                line.addEventHandler(KeyEvent.ANY, new EventHandler<KeyEvent>() {
                                    @Override
                                    public void handle(final KeyEvent keyEvent) 
                                    {
                                        System.out.println("key handler");
                                    }
                                });
                It does not work.
                • 5. Re: Event on Del key keyboard
                  James_D
                  Your code adds a handler for KeyEvents to the line; but the line will not generate any key events unless it has the keyboard focus.

                  You need to arrange for the particular line you want to delete to have the keyboard focus when the delete key is pressed. Try calling
                  line.requestFocus();
                  when, for example, you add a new line. Then the delete key should delete the most recently added line, as long as keyboard focus has not moved since it was added.
                  • 6. Re: Event on Del key keyboard
                    935521
                    Hi James D,

                    your suggestion solve my problem, thank you very much!