Jtextfield checking input

hi all, i was wondering what may be the best way to check input. the current method i'm using is to handle the keyTyped event. for example i do not want to allow the user to enter letter "a":
         @Override
            public void keyTyped(java.awt.event.KeyEvent e) {
                if (e.getKeyChar()=='a') {
                    e.consume();
                }but the user can still copy and paste the letter into the text field. is there a proper way to check input which occurs into the TextField and acting on it ?

Pacerier wrote:
however if i'm right, not all documents have a document filter right?The reality of things is that all Document implementations shipped with the JRE extend AbstractDocument and can take DocumentFilters, so this is a non-issue. Further, Sun will never change the default Document type installed on the various text components, as that would break backwards compatibility.(1)
If you're super paranoid you can always ensure your Document takes a DocumentFilter like so:
JTextField field = new JTextField();
PlainDocument doc = new PlainDocument();
doc.setDocumentFilter(new MyDocumentFilter());
field.setDocument(doc);Most of the time people will just code defensively, like so:
Document doc = textComp.getDocument();
if (doc instanceof AbstractDocument) {
   AbstractDocument ad = (AbstractDocument)doc;
   // Do AbstractDocument-specific stuff
}knowing that the conditional should always be true.
(1) Technically they could, since JTextComponent only refers to its Document by interface, but the reality of it is that they won't, as too much code has been written around text components having PlainDocuments or DefaultStyledDocuments (whichever is appropriate), or at the very least AbstractDocuments.

Similar Messages

  • How to make jTextField 's input unscrollable?

    Dear All
    i create a JTextField, i want to set it inputsize = 5(charaters)
    i am doing like this
    JTextField jTextField = new JTextField(5);
    jTextField.setScrollOffset(0);
    but it doesnot work out
    it is still scrollable, i main i could input more than
    5 charaters
    How to fix JTextField's input size?
    Thx in advance

    Hi,
    that does not change the amount of characters, you can input, it only says, that the JTextFields-size should be 5 times as wide as an average char.
    If you want to limit the characters you will have to replace the Plaindocument JTextFields uses internally with a subclass of it, that overwrites its insertString(...)-method. An example, that makes all input upcase is found in the documentation of JTextField. You can do your checking in a similar way there - just checking if the length of the string to insert plus the length of the text that is currently in the box is greater than the maximum length - if it is not, pass all parameter simply to the super.insertString(...)-method - if it is greater, that only do a Toolkit.getDefaultToolkit().beep().
    Also there are many topics dealing with this stuff - please search the forum for it.
    greetings Marsian

  • Check input  values are available in Hex

    how to check input values are available between 0x0000000000000001 to
    0xFFFFFFFFFFFFFFFE
    i am using following code. but error will come
    public boolean hexCheck16(String hex)
    String hex_min = "0x0000000000000001";
    String hex_max = "0xFFFFFFFFFFFFFFFE";
         int min1 = Integer.parseInt(hex_min.substring(2).toString(),16);
         int max2 = Integer.parseInt(hex_max.substring(2).toString(),16);
         int num = Integer.parseInt(hex.toString(), 16);
         if(num >= min1 & num <= max2)
         return true;
         else
         return false;
    Error is : java.lang.NumberFormatException: For input string: "FFFFFFFFFFFFFFFE"
    how to solve?

    Use BigInteger (and lose the "0x").

  • Please check inputs - error message

    Hi,
    I'm using Microsoft Windows XP Profesional SP3, english version
    I installed all needed SDKs correctly (all was done like in tutorial movies)
    I'm able to create Windows Mobile package and this package works correctly, but when i try to create Symbian60 package I always get message 'Error occured while packaging. Please check inputs and try again'. I tried to reinstall Symbian SDK and also Adobe mobile packager 1.1 but this not helps. I would like to ask if there is some log file craeted during packaging. I think that problem is somewhere in SymbianSDK but I can not find any log or any process file.
    please help
    I'm attaching screenshot with my input data. All input files are correct.
    mi4k

    Easily add this option to below file
    file;
    D:\oracle\sqldeveloper\ver3.1.07.42\sqldeveloper\bin\sqldeveloper.conf
    java VM option;
    AddVMOption -Duser.timezone="+07:00"

  • Cl_gui_alv_grid check input Data

    HI,
    I have a problem trying to implement an ALV with editable cells using  cl_gui_alv_grid class.
    My problem is : how to check input data ?
    More precisely, in field catalog, I can specify an control table and a control field for each ALV column. If I do so, a search is displayed for the corresponding field and if I choose a value not available in this serach help, an error popup message is displayed and the cell in error is colored with a red line.
    IT works perfectly for undepdant fields.
    I try to do the same for field DISPO of table MARC displayed in my ALV. The sytem display appropriate serach help but alaways disaply an error message telling that "DISPO has not been found for plant ..." and it does not found the appropraite valeu of field WERKS/PLANT. HOw to correct this problem. A precision : the field WERKS is part of displayed column and is filled with a correct value but the link is not done.
    Thanks for help
    Regards
    Morgan

    Check the sample programs
    BCALV_EDIT_01 - Editabale ALV
    BCALV_EDIT_02 - Edit, save and F4 help
    BCALV_EDIT_03 - Events handled
    BCALV_EDIT_04
    BCALV_EDIT_05
    BCALV_EDIT_06
    BCALV_EDIT_07
    BCALV_EDIT_08

  • "Check Input Output Prompt" when trying record master.

    Been going through the ringer on Print to Video w Final Cut Express 4 to my Canon XHA1 1080i. Finally got rid of the "Transcoding to HDV" prompt after converting file to QuickTime and changing my sequence settings.  My VCR signal set up is correct (checked w Canon)  and my firewire cable is fine.  Still receiving "Check Input Output" when I go to record on camera.  Any suggestions would be greatly appreciated! 

    It's been seven (7) days since I filed a suport case, no reply from WD...The way this is going the drive will soon end up at a recycling station after it has been smashed with a hammer. :-(Which means I will be in the market for a non-WD NAS very soon, they don't seem to support their customers very well. Buggy firmware, ages between fixes, inability to actually upgrade, and lack of support. Disappointed to say the least.

  • UDF Required for checking input value.

    Hi,
         I'm in need of an UDF for checking the input value and pass constant based on that.
    Requirement.
                        We are having 1 field on the source side which need to be checked.
                        If the first character of the input is alphabet (No case sensitive)
                        then pass a constant "X" to the output field.
                        Other wise donot create that particular element.
    Example : If INPUT = A12345 then OUTPUT = X
                    If INPUT = 12345D then OUTPUT element must not be created.
    Regards,
    Swapna

    use the following code in a udf and then pass the result to createIf standard function to help you with the requirement.
    String chkValue = input.subString(0,1);
    String flag = "false";
    if ((chkValue >= 'A' && chkValue <= 'Z') || (chkValue >= 'a' && chkValue <= 'z')) {
    flag = "true";
                          return flag;
    else
    {return flag;}

  • Work with JTable cell (check input, set text color)

    I have a JTable with data
    The task is:
    User inputs some data into cell
    I want to check his input string for equatily with other values in column
    For example, I have these values in column:
    1.example
    2.exercise
    3.execute
    User inputs word "execution" in the same column
    until user inputs "executi" his input string should be red.
    What event I have to listen to if I want to get user input string?
    Also a question:
    Is there any opportunity to work with cell row (copy it to another JTable) ?
    Message was edited by:
    Holod

    i think you should use cell editor as textfield (although that is default for jtable) and then implements document listener and set text color in insert update method.
    try this out :-)

  • Error checking input text fields.

    Hi
    Just wondering where I find a list of what can be error checked in input text fields?
    I'm looking for things like...
    - checking that letters and not numbers are entered
    - checking that an entered number is in a certain range
    etc...
    Thanks guys
    Shaun

    To restrict input for a text field, use the following AS3 codes:
    myInput.restrict = "A-Z, a-z, ., ,'"; //myInput is the instance name of your text area
    To check the range of the numbers, use the following code:
    myInput.restrict = "0-9";
    myInput.maxChars = 3;
    myBtn.addEventListener(MouseEvent.CLICK, chk);
    function chk(e:MouseEvent):void
        var myString:String = myInput.text;
        var i:Number = Number(myString); //Converting the string to a number variable
        var max:Number = 100; //Maximum range
        var min:Number = 30; //Minimum range
        if (i < max && i > min) //Condition to indicate the range
            trace("Within range");
        else
            trace("Out of range");
    Hope this helps.

  • A/c doc not created and error msg "Business Area Miss-Match Check input"

    Hi,
    After creation of new Plant and Business area create a invoice , But my accounting document does't created and error msg show "Business Area Miss-Match Please Check Your Input". plz suggest me.
    Regards,
    Sohail

    Dear Sohail Rahman,
    Looks like the Business area has not been properly assigned.
    Business area can be created for three combinations:
    1. Business area by sales area
    2. Business area by plant and division
    3. Business area by plant and item division
    In your case, check whether the business area is maintained for the combination of plant/division and plant/item division.
    Hope this helps
    Thanks
    Murtuza

  • Checking input before sending request

    Hello JSP-fellows,
    I have an jsp-page with a number of input fields which I would like verify before I send the request.
    Is there any possibility that after pressing the submit button I go to
    a java script input check and if the check is not OK I do not send the request to the server?
    My submit button tag looks like this (the ordinare way):
    <td ><input type="submit" value="Submit" name="submitButton"></td>
    Can I, in some way, embed an onClick() script in the tag above and not execute the submition in case any input field is wrong?
    Perhaps there are some other better ways to do an input check in an jsp page before sending request?
    Thanks in advance for you help,
    Best regards
    Danny

    Not really a JSP issue, more general HTML.
    But, yes you can. You can, like you already wrote, embed an onclick in a submit button. A better approach, in my opinion, is to use the onsubmit function of the FORM tag:
    <FORM action="..." onsubmit="return validate();" >
    Let the validate function return false if the validation wasn't successfull, then the form will not be posted.

  • JTextField Number Input

    Hello.
    How do you limit the number of characters in a JTextField.
    For example: JTextField first_box_serial=new JTextField(4);
    Will still allow the user to inter as many characters in the field.
    I want the user to only be able to enter 4 characters and thats it!!
    Thank You in advance!!

    Use this:
    public class LimitedLengthDocument extends javax.swing.text.PlainDocument {     
         int maxLength;
         public LimitedLengthDocument(int maxLength) {
              super();
              this.maxLength = maxLength;
         public void insertString(int offset, String str, AttributeSet a)
              if(getLength() < maxLength)
                  super.insertString(offset,str,a);
    }Use setDocument(new LimitedLengthDocument(4)); on your JTextField. You'll only be able to type in 4 characters. To only allow numbers you can just modify insertString to check that str is numbers.
    Jeff

  • Checking input as Integer

    HI,
    can any body tell me that how can i check that an input form a jsp(page1) is number or not ?
    page1.jsp
    <form method = "POST" action = "page2.jsp">
    input type = "text" name = "number"
    input type = "submi"t value = "submit"
    </form>
    page2.jsp
    Now here i want to check that the input is integer or not
    How can i do that
    thanks

    Well you have it as a string right?
    String enteredValue = request.getParameter("number");
    // Is the value there?
    if (enteredValue == null){
      // no value entered.  error?
    else {
      // check if it is an actual number
      Integer enteredNumber = null;
      try{
        enteredNumber = Integer.valueOf(enteredValue);
      catch(NumberFormatException e){
        // not a valid integer
    }

  • JtextField , mask input and setText() ...

    ... I use two JTextFields on wich i put a LimitedStyle Document in order to only allow the user
    to input dates in a specific format. Unfortunally, this input mask seems to block the setText()
    function of the JTextField. I can't set the Field to the current DateSytem regarding the date format
    i use (the date is in the right format input). Does someone know how to bypass this problem ?
    If needed i can post the code source of the Document.
    Thanks in advance,
    Marc.

    jTextField.getDocument().setText()
    dosen't work because there is no setText(0 method on document.
    instead i am doing this
    Document docm = new ShowDocument(userInput);
    ShowDocument is a class which overrides insertString() method
    import javax.swing.text.PlainDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    public class ShowDocument extends PlainDocument
    String st=null;
    public ShowDocument(String s)
    this.st = s;
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
    super.insertString(offs, st,a);
    return;
    this works fine.

  • How to Check input

    HI
    i wont to do a test program  and i have rfc that in import i have pernr and if user put pernr he get table with employee name and... the problem is when user put vaild pernr i wont to bring a popup that pernr dosent exist .
    how i do that?
    Regards

    in this case u have to make of expections/return tables.
    if pernr is initial.
    get data for all .
    elseif.
    select pernr from it03 into table it3
    where pernr =  pernr.
    if sy-subrc ne 0.
    raise expections/ update error return tables .
    endif.
    regards
    Peram

Maybe you are looking for