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 7171871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:30:01+00:00 2026-05-28T15:30:01+00:00

I’m doing an application that must read some characters via keyboard and interpret them.

  • 0

I’m doing an application that must read some characters via keyboard and interpret them.

To capture the keys, I open a JDialog and set a KeyEventDispatcher, so I can capture the characters in the dispatchKeyEvent method. In the JDialog there’s a button that removes the KeyEventDispatcher and disposes of the JDialog.

There are two problems with it:
– The first time the JDialog is opened, it’s like the KeyEventDispatcher was not set
– As I close and open this JDialog, the KeyEventDispatchers are cumulating (open he 1st time, there’s none running; open the 2nd time, there’s one running, open the 3rd time, there’re 2 running, …)

It seems that the KeyEventDispacthers are being set when the JDialog is closed and not removed, instead of being set when the JDialog is opened and removed when it closes.

Someneone could please help me understand what’s happening and how I can fix it?

Here’s a simplified version of the JDialog class (with only the key capture part):

public class SequenceDialog {
    private JDialog dialog;
    private JButton finishButton;
    private DialogKeyEventDispatcher keyEventDispatcher;

    public SequenceDialog() {
        initializeDialog();
    }

    private void initializeDialog() {
        dialog = new JDialog();
        finishButton = new JButton("Finish");

        finishButton.addActionListener(new FinishButtonListener());
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
        dialog.add(finishButton);
        setKeyListener();
        dialog.setVisible(true);
        dialog.pack();
    }

    /** Adds the KeyEventDispacther */
    private void setKeyListener() {
        keyEventDispatcher = new DialogKeyEventDispatcher();

        KeyboardFocusManager manager = KeyboardFocusManager
                .getCurrentKeyboardFocusManager();
            manager.addKeyEventDispatcher(keyEventDispatcher);
    }

    private class FinishButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //Removing the KeyEventDispacther
            KeyboardFocusManager manager = KeyboardFocusManager
                    .getCurrentKeyboardFocusManager();
                manager.removeKeyEventDispatcher(keyEventDispatcher);

            dialog.dispose();
        }
    }

    /** The KeyEventDispatcher to be executed */
    private class DialogKeyEventDispatcher implements KeyEventDispatcher {
        public boolean dispatchKeyEvent(KeyEvent e) {
            if(e.getID() == KeyEvent.KEY_PRESSED) {
                System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
            }

            return false;
        }
    }
}

If there’s another way of capturing the keys, I will gladly see it. So far, what I have tried:

  • KeyListener, didn’t work even when I added it to the JButton and the contentPane
  • KeyEventPostProcessor, had the same effect as using KeyEventDispatcher
  • KeyBinding, didn’t work and doesn’t seem like the best choice, as I must capture everything typed
  • 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-28T15:30:02+00:00Added an answer on May 28, 2026 at 3:30 pm

    Can’t reproduce the not-working on first.

    Can reproduce the stacking: the dispatcher is not removed when closing the dialog by clicking on the close icon in the title. In this case, strokes typed in the main frame are printed after the dialog has been closed.

    The dispatcher can be reliably removed by so in both dispose and a WindowListener (instead of in the finish action):

        private void initializeDialog() {
            dialog = new JDialog() {
    
                @Override
                public void dispose() {
                    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    manager.removeKeyEventDispatcher(keyEventDispatcher);
                    LOG.info("disposed: " + manager);
                    super.dispose();
                }
    
            };
            WindowListener l = new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    manager.removeKeyEventDispatcher(keyEventDispatcher);
                    LOG.info("closing: " + manager);
                }
    
            };
            dialog.addWindowListener(l);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.