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

  • Home
  • SEARCH
  • 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 6319033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:44:02+00:00 2026-05-24T15:44:02+00:00

What is the best way to implement a press x to continue type of

  • 0

What is the best way to implement a “press x to continue” type of thing in Java?

Specifically, I have a custom class which extends JFrame and a custom class which extends JPanel. I have a Main.java (which has an instance of my JFrame class), and there is a point where I do not want to continue until the user has pressed the space bar:

Main.java:

...code...
frame.waitForSpace();
...more code which gets executed only after space is pressed...

So, in my frame class, how should I implement this:

MyFrame.java:

/* This method only finishes when the space bar is pressed */
public void waitForSpace() {

}

By the way, I have the KeyListener on my JFrame which works when the space button is pressed. But I’m not quite sure how to incorporate that into what I’m trying to do.

Example code would be awesome!

  • 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-05-24T15:44:04+00:00Added an answer on May 24, 2026 at 3:44 pm

    Using a CountDownLatch when await() is invoked, the current main() thread will wait until the countDown() method is called from another thread.

    The KeyEventDispatcher lets you add a keystroke listener globally. So whenever a key is pressed dispatchKeyEvent() gets called from the EDT (Event Dispatching Thread) and there you can check for ‘space’ being pressed and release the countdown latch that main() is waiting on.

    However, if the thread that calls waitForSpace() is the EDT, then you will be forcing the EDT to wait for itself, causing deadlock. It is impossible to wait on the EDT and still receive key events as far as I know.

    This will work so long as a JFrame has focus:

    public void waitForSpace() {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            // Anonymous class invoked from EDT
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE)
                    latch.countDown();
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        latch.await();  // current thread waits here until countDown() is called
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is the best way to implement a sparse vector in Java? Of course
What is the best way to implement web services in PHP? I have heard
What is the best way to implement the following scenario in Java: A sort
Which is the best way to implement a logoff button in Asp.Net? I am
what is the best way to implement C++ functionality in Java? Currently I call
What's the best way to implement a stack using linked lists in Java? EDIT:
What is the best way to implement server push for more than one thing.
What is the best way to implement a StackOverflow-style Recent Activities page? I have
Which is the best way to implement factory method. 1) A factory method that
What is the best way to implement a UINavigationBar that has a custom background

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.