Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Updated 26-Apr-11 14:26pm

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
Share this answer
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! :-)

Share this answer
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.
Sergey Alexandrovich Kryukov 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 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:
Sergey Alexandrovich Kryukov 27-Apr-11 14:42pm    
Not sure. I think it was my fault. Not a big deal I hope... :-)
Thank you, Sandeep.
Sandeep Mewara 28-Apr-11 0:30am    
Hey it's ok. :)
- 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()

Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900