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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:22:16+00:00 2026-05-24T06:22:16+00:00

I want my JTextField to process the text not only when ENTER is pressed,

  • 0

I want my JTextField to process the text not only when ENTER is pressed,
but also when SPACE is pressed. You can see in the code below that I associated the action that is usually associated with ENTER to SPACE, but I get some unexpected behavior (see below).

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class Test extends JFrame {

    private JTextField textField;

    public Test() {
        textField = new JTextField();
        add(textField);
        InputMap inputMap = this.textField.getInputMap();
        Object actionSubmit = inputMap.get(KeyStroke.getKeyStroke("ENTER"));
        Object actionSubmitSp = inputMap.get(KeyStroke.getKeyStroke("SPACE"));
        System.out.println("actionSubmit for space = " + actionSubmitSp);
        ActionMap actionMap = this.textField.getActionMap();
        Action action = actionMap.get(actionSubmit);
        System.out.println("actionSubmit = " + actionSubmit);
        textField.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
                actionSubmit);

        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                textField.setText(null);
                System.out.println("event received:[" + 
                        evt.getActionCommand() + "]");
            }
        });
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Test test = new Test();
                test.pack();
                test.setVisible(true);
            }
        });
    }
}

If I type “x SPACE” an ActionEvent is produced and the JTextField is cleared.
However the refreshed JTextField
is not a “null” string as requested, but ” “. The SPACE from the previous action
has “leaked” to the refreshed JTextField, which is quite annoying.

I looked in the swing code a bit. My best guess is that an ActionEvent is generated from some KeyEvent’s, and KeyEvent.isConsumed() has different consequences depending if the KeyEvent was a ENTER or a SPACE (an ENTER is swallowed, but not a SPACE).

Anyone knows how to fix this? Or knows a different method to accomplish my goal?

  • 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-24T06:22:17+00:00Added an answer on May 24, 2026 at 6:22 am

    Multiple events are being generated. Your code is being executed on a keyPressed event. However using the space bar also results in a keyTyped event being generated. This is handled by the text component after the keyPressed code has been executed, so the Document is cleared and then a space is added to it.

    Anyone knows how to fix this?

    Don’t use a KeyListener.

    Add your code to the end of the EDT so that is executes AFTER the Document has been updated with the space:

    textField.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(final ActionEvent evt)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    textField.setText(null);
                    System.out.println("event received:[" + evt.getActionCommand() + "]");
                }
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to reference a COM DLL in a .NET project, but I also
I want to have a JTextField (or another text-like input which supports focus and
I want to position a JDialog box below a JTextField of a JFrame and
I want to loop over the contents of a text file and do a
Question: I want to control cmd.exe from winforms. I DO NOT mean every command
x = new JTextField(); count1 =22; String s1 = new Integer(count1).toString(); x.setText(s1); How can
Assume we want to validate user input, while user is typing in a JTextField.
I have a JTextField with a documentListener on it. I want to change the
I have a JToolTip attached to the onclick event of a JTextField. I want
How do I manipulate textfield variables within an iPhone app? I want to have

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.