2

I've created the following function in order to close any pop up Stage in my program:

public void escapeKeyPressed(final KeyEvent keyEvent , Stage diolog) { if (keyEvent.getCode() == KeyCode.ESCAPE) { diolog.close(); } } 

Then, I have a clickable image where can be clicked and a form will pop up :

The problem is when I call the function I get error for the first argument . Here is how I call it ;)

 escapeKeyPressed( KeyCode.ESCAPE ,dialog ); 

Does any body know how can I fix that ?

1 Answer 1

3

Your method signature is (final KeyEvent keyEvent, Stage diolog) and you pass a KeyCode as first argument which is not a KeyEvent.

You can pass the original KeyEvent instead, to fulfil the signature as:

yourPopUp.setOnKeyPressed((KeyEvent event) -> escapeKeyPressed(event, dialog )); 

But it would be much cleaner if you would update the method signature to accept the KeyCode directly:

public void escapeKeyPressed(KeyCode keyCode , Stage diolog) { if (keyCode == KeyCode.ESCAPE) diolog.close(); } 

In this case you can have the original call as:

escapeKeyPressed(KeyCode.ESCAPE, dialog ); 
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried the cleaner version ;) and I am getting no syntax error anymore but once the stage pops up in a matter of second it gets closed automatically and doesnt wait for me to click the escape button :D
As this question was about the problem with calling your function and this question was answered, I would propose to please ask another question with a Minimal, Complete, and Verifiable example (e.g. the code for one of your images) where the actual functionality of your listener can be fixed :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.