Click here to Skip to main content
13,090,178 members (49,564 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi everyone.

i have a problem pertaining to text box that do not accept any characters.
Here is the situation, i have a text box that do not accept any characters, i have an onkeyReleased event that whenever i press any key in keyboard, it says, "Ooops..,this text box do not accept any characters!". How can i do that?

Please help me.
Posted 26-Apr-11 14:23pm
Updated 26-Apr-11 14:26pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Your class should implement keyListener adapter to do so,
public class KeyListenerTester extends Frame implements KeyListener

Then implement,
public void keyReleased(KeyEvent Event) {
   int key=evt.getKeyCode();  ///using validation here
                              /// ascii code A=65 ,Z=90 ,a=97 ,z=122 
       ///making Textbox empty
       JOptionPane.showMessageDialog(frame,"Ooops..,this text box do not accept any characters!.");

Try it,
its just suggestion then reply
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Showing this is easy, but I feel you need the code to behave accordingly, to prevent characters from entering.
This is a bit less than trivial. You will need to use onKeyPress instead.

      <script type="text/javascript"><!--
         function filterOut(eventInstance) {
            var character = String.fromCharCode(eventInstance.which);
            if (eventInstance.which == 8)
               character = "backspace";
            alert("You want to enter " + character +
               ", Guts_ryo? Relax, this text box do not accept any characters!"); 
            eventInstance.returnValue = false;
            return false;
         } //filterOut
<input type="text" onkeypress="filterOut(event)"/>

Pay attention for special processing of backspace. You can use this code to filter out some character sets to get, say, digits only or something like that. Don't forget to allow backspace in all cases.

Sorry, I misread it; though it was "Javascript"! Oops! :-)

Sandeep Mewara 27-Apr-11 1:35am
Comment from OP:
Thanks SAKryukov for the help.

I know how to do that in javascript but i need it in swing method.
Any suggestion is accepted.

Please help.
Sandeep Mewara 27-Apr-11 1:36am
I see your answer, this happens when OP does not tag question properly and explain what is he trying and stuck up with. :doh:
SAKryukov 27-Apr-11 14:42pm
Not sure. I think it was my fault. Not a big deal I hope... :-)
Thank you, Sandeep.
SAKryukov 27-Apr-11 14:48pm
@Guts_ryo: sorry about it. I do understand what you need. Answering a wrong question was my fault. (With Swing the idea would be very close, but the code is not the same, of course.) Sorry, I don't even have the environment to implement it...

Please check the code provided by Torsten.
Sandeep Mewara 28-Apr-11 0:30am
Hey it's ok. :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

- add a KeyListener to the Component you're entering the text in.
- The KeyListener has to implement KeyListener and therefor the method: public void keyReleased(KeyEvent oEvent) {/* doany(); */}
- the KeyEvent provides the source of the event and therefor the entered text.
- update your textbox with the won data.

public void keyReleased(KeyEvent oEvent) {
    Control oControl = (Control)oEvent.getSource();
        final Text oText = (Text) oEvent.widget;
        if ((oText.getText() == null) || oText.getText().equals("")) {
            // setDefaultText (like "")
        } else {
            // use String from oText.getText()


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 27 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100