Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing
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 15:23pm
Edited 26-Apr-11 15:26pm
v2
Rate this: bad
good
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 
   if((key>=65&&key<=90)||(key>=97&&key<=122))
   { 
       ///making Textbox empty
       JOptionPane.showMessageDialog(frame,"Ooops..,this text box do not accept any characters!.");
   }
Try it,
its just suggestion then reply
  Permalink  
Rate this: bad
good
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.
 
See:
<html>
   <head>
      <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.preventDefault();
            eventInstance.returnValue = false;
            return false;
         } //filterOut
      --></script>
   </head>
<body">
 
<input type="text" onkeypress="filterOut(event)"/>
 
</body>
</html>
 
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! Smile | :)

 
—SA
  Permalink  
v4
Comments
Sandeep Mewara at 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.
SAKryukov at 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.
--SA
Sandeep Mewara at 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 at 27-Apr-11 14:42pm
   
Not sure. I think it was my fault. Not a big deal I hope... :-)
Thank you, Sandeep.
--SA
Sandeep Mewara at 28-Apr-11 0:30am
   
Hey it's ok. :)
Rate this: bad
good
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()
        }
}
 
regards
Torsten
  Permalink  
v2

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

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 27 Apr 2011
Copyright © CodeProject, 1999-2014
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