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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:15:52+00:00 2026-06-12T18:15:52+00:00

I put a KeyListener on a TextField in my swing application to try some

  • 0

I put a KeyListener on a TextField in my swing application to try some functionalities. The goal is to react on every key typed in that TextField. The user should only type in numbers, but how it is, it is possible to enter alphabetical chars too. So additionally I have to check every time after a key is typed, if the whole thing is a number, if so, make something with that number, if not, tell the user there is an error without exit the program. So I want to do something like this:

String enteredNumPlayers = "";

JTextField textfieldNumPlayers = new JTextField();
textfieldNumPlayers.setBounds(/*some values*/);
textfieldNumPlayers.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.DARK_GRAY));
textfieldNumPlayers.setHorizontalAlignment(JTextField.CENTER);
textfieldNumPlayers.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        if(e.getKeyChar()!=/*Code of the back key*/){
            enteredNumPlayers += e.getKeyChar();
            System.out.println(e);
        }else{
            enteredNumPlayers = enteredNumPlayers.substring(0, s1.length()-1);
        }

        try{
            Integer.parseInt(enteredNumPlayers);
            // do something with that number
        }catch (NumberFormatException err){
            new ErrorDialog("Not a number"); // my own method to allude user
        }   
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }
});

Now I wanted to look whats the specific Code for the back key by simply System.out.println(e) in the keyTyped(...) method, but following get printed:

java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unbekannt keyCode: 0x0,keyChar=Rücktaste,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on (...)

why is every key value or code = 0 or unknown? Shouldn’t it be the ascii value? Using “Rücktaste” would also be ugly, since on a english working computer this value would be different, isn’t it? So how can I cleanly check if the key typed is the back key?

The same thing happens with other characters, except that their keyChar is the right one.

  • 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-12T18:15:53+00:00Added an answer on June 12, 2026 at 6:15 pm

    What you are looking for is a DocumentFilter, DocumentListener or a JFormattedTextField. All three of them are a better solution then using a key listener as it also covers drag-and-drop, copy-paste or any other mechanism you can think of to put text into a textfield.

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

Sidebar

Related Questions

I put some quotes and short messaged in the web-based application that I am
To put it simple, there's a simple java swing app that consists of JFrame
I put together a fairly simple Mono for Android application that does nothing more
Put it simply: I'd like to let the user to select some tags in
Put value as a field makes sense only if this one represents some object's
I put together this quick carousel that I'm using to cycle through different divs
Put your foot inside the boots of an expert developer that is new to
To put it simply, if process A starts application B. Whenever application B tries
Simply put, I have a website where you can sign up as a user
I put together some VBA code for Outlook 2007 which has been working predominantly

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.