Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:57:16+00:00 2026-06-11T23:57:16+00:00

I’m trying to have my dialog accept <Enter> for the defaultButton and <ESC> to

  • 0

I’m trying to have my dialog accept <Enter> for the defaultButton and <ESC> to execute the cancelButton.

I have some code like this:

dialog1 = new Stage(StageStyle.UNDECORATED);
dialog1.initModality(Modality.WINDOW_MODAL);
dialog1.initOwner(primaryStage);
dialog1.setScene(
  new Scene(
    HBoxBuilder.create().styleClass("modal-dialog").children(
      LabelBuilder.create().text("Tells user what to do...").build(),
      ButtonBuilder.create().text("Next step").defaultButton(true).onAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent actionEvent) {
          // take action and close the dialog1.
          // do "OK" actions here...
          dialog1.close();
        }
      }).build(),
      ButtonBuilder.create().text("Cancel").cancelButton(true).onAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent actionEvent) {
          // abort action and close dialog1.
          // do "CANCEL" actions here...
          dialog1.close();
        }
      }).build()
    ).build()
    , Color.TRANSPARENT
  )
);
dialog1.getScene().getStylesheets().add(getClass().getResource("ModalDialog.css").toExternalForm());

And I’d like to add something like the below to handle the keyboard. Is something simple like this possible?

.onAction(new EventHandler<KeyEvent>() {
  @Override public void handle(KeyEvent t) {
    if (t.getCode() == KeyCode.ENTER) {
      // do "OK" actions here
      dialog1.close();                    
    } else {
      // do "CANCEL" actions here
      dialog1.close();                    
    }
  }
}).build()        

My problem is, I have looked around for a ‘node’ to hang this handler on, but can’t seem to find one. Like TextInputBuilder, or something like that. I also don’t know the exact format of the syntax, even if a TextInputBuilder was the correct node to create, so if you’d show me the exact form that the call should take, that’d be great.

I am guessing something like:

dialog1.setScene(
  new Scene(
    HBoxBuilder.create().styleClass("modal-dialog").children(
      TextInputFieldBuilder.onAction(new EventHandler<KeyEvent>() {
        @Override public void handle(KeyEvent t) {
          if (t.getCode() == KeyCode.ENTER) {
            // do "OK" actions here
            dialog1.close();                    
          } else {
            // do "CANCEL" actions here
            dialog1.close();                    
          }
        }
      }).build(),
      LabelBuilder.create().text("Tells user what to do...").build(),
      // [ ... ]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:57:17+00:00Added an answer on June 11, 2026 at 11:57 pm

    I’m trying to have my dialog accept <Enter> for the defaultButton and <ESC> to execute the cancelButton.

    This is a the default behaviour of the defaultButton and cancelButton, so usually no extra code would be required to do this.


    If, in the unlikely event, that some other control in the scene is consuming or presses and you really want to override that:

    1. Add a filter on the scene to trap the relevant KeyEvents.
    2. Invoke the fire event on the corresponding button when you receive it.
    3. consume the event after you have processed it by invoking fire so that the event won’t get routed to the button handler causing the event to potentially be handled twice.

    Don’t duplicate code for ok/cancel and dialog closing in the key event handler which is already defined for the buttons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.