Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Adding mouse event for secondary mouse button Javafx

so i have this anchorpane where i wish to add a mouse listner for the Secondary mouse key ive tried the following but i keep getting an error anyone know what the problem is?

   mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseButton>() {

                    @Override
                    public void handle(MouseButton event) {
                        System.out.Println("Works");

                    }
                });

for the record i have also tried this:

            mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    System.out.println("WOrks");
                }
            });

Stack trace:

Bound mismatch: The generic method addEventHandler(EventType, EventHandler) of type Node is not applicable for the arguments (MouseButton, new EventHandler(){}). The inferred type MouseButton&Event is not a valid substitute for the bounded parameter

And the other:

Bound mismatch: The type MouseButton is not a valid substitute for the bounded parameter of the type EventHandler

Upvotes: 3

Views: 2945

Answers (1)

Reimeus
Reimeus

Reputation: 159774

There is no EventType based on MouseButton.SECONDARY. You need to check the MouseEvent itself:

mainDisplayPanel.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        if (event.getButton() == MouseButton.SECONDARY) {
           System.out.println("Works");
        }
    }
});

Upvotes: 5

Related Questions