How to limit characters entered in JTextField

Hi,
How can we limit character entered in JTextField.
1.) I know its posssible with JFormatedTextField...
2.) I can do this by creating a child class of JTextField.....
But there must be an easy way (sun guys would have included some methods to limit characters entered)
Any one know any such methods...?
Thank You
Ashish P.J

Read the API for JTextcomponent and follow the link to the tutorial where you'll find a working example of using a DocumentFilter to achieve this.
db

Similar Messages

  • How to limit to enter a IP address in TextField?

    Hello everyone,
    I have a PDF form which users need to enter a IP address. In order to get a correct IP address, I need to limit a TextField's pattern.
    What my boss want me to realize is like the picture below:
    It means when users fill 3 numbers of IP address, it will jump to next place and display a point "." automatically.
    But I found it can't change the rawvalue of the TextField when it is in a typing status(in Change event).
    Do you think if I can realize this requirement? How can I do ?
    Thanks a lot!
    Elly

    Hi,
    you can use a regular expression to verify the entered text.
    This script in the fields exit event, will check the entered text against a regular expression.
    if (! this.rawValue.match(/\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/ig)) {
         xfa.host.messageBox("invalid IP-Address");
    Here`s a source for regular expression you can try.
    RegExr: Learn, Build, & Test RegEx

  • How to limit characters

    I've got DW8 retrieving a text chunk from SQL Server/ASP and
    I need to
    restrict it to the first 300 characters. Is there a way in DW
    to do this?

    Use this for your source
    SELECT LEFT(mycolumn, 300) FROM mytable
    Depending on the datatype of the column, you may have to
    convert (CAST) to varchar first.

  • How to limit the number of characters entered in a JTextfield???

    Hello there,
    I have created a Text box using swing component JTextField and I want to limit the number of characters entered to 8 characters..how do i proceed with that?
    Thanks in advance!
    Joe

    Ty out this
    import com.sun.java.swing.text.*;
    //import javax.swing.text.*;
    public class JTextFieldLimit extends PlainDocument {
    private int limit;
    // optional uppercase conversion
    private boolean toUppercase = false;
    JTextFieldLimit(int limit) {
    super();
    this.limit = limit;
    JTextFieldLimit(int limit, boolean upper) {
    super();
    this.limit = limit;
    toUppercase = upper;
    public void insertString
    (int offset, String str, AttributeSet attr)
    throws BadLocationException {
    if (str == null) return;
    if ((getLength() + str.length()) <= limit) {
    if (toUppercase) str = str.toUpperCase();
    super.insertString(offset, str, attr);
    import java.awt.*;
    import com.sun.java.swing.*;
    //import javax.swing.*;
    public class tswing extends JApplet{
    JTextField textfield1;
    JLabel label1;
    public void init() {
    getContentPane().setLayout(new FlowLayout());
    label1 = new JLabel("max 10 chars");
    textfield1 = new JTextField(15);
    getContentPane().add(label1);
    getContentPane().add(textfield1);
    textfield1.setDocument
    (new JTextFieldLimit(10));

  • How do I limit number of characters in a JTextField?(different from others)

    Hi,
    I know this question has been posted several times on this forum, but my question is actually a different one. So please read.
    I have the following code that puts two JTextFields in a JFrame. I want to limit the number of characters in the first JTextField to three characters. What's different about my question from other solutions provided, is that I don't want to be able to type more than three characters in the JTextField, and then not be able to get out of JTextField unless I have three or less characters. What I do want, is to have maximum of three characters, not be able to type the fourth, and have access to the other JTextField at all times.
    I have tried the following solution of setting an InputVerifier to my JTextField, but it hasn't worked, since I can still type the fourth character.
    Looking forward to your help!
    Thanks.
    import java.awt.Dimension;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.InputVerifier;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    public class TextFieldVerifier {
         public TextFieldVerifier()
              JFrame frame = new JFrame("My TextVerifier");
              JPanel panel = new JPanel();
              JLabel firstTextFieldLabel = new JLabel("First: ");
              JLabel secondTextFieldLabel = new JLabel("Second: ");
              final JTextField myTextField = new JTextField();
              myTextField.setPreferredSize(new Dimension(100,20));
              myTextField.setInputVerifier(new InputVerifier()
                   public boolean verify(JComponent input) {
                        if(myTextField.getText().length()<=3)
                             return true;
                        else
                            return false;     
              myTextField.addKeyListener(new KeyAdapter()
                   public void keyTyped(KeyEvent e)
                        if(!myTextField.getInputVerifier().verify(myTextField))
                             myTextField.setText(myTextField.getText().substring(0, myTextField.getText().length()-1));
              JTextField secondTextField = new JTextField();
              secondTextField.setPreferredSize(new Dimension(100,20));
              panel.add(firstTextFieldLabel);
              panel.add(myTextField);
              panel.add(secondTextFieldLabel);
              panel.add(secondTextField);
              frame.getContentPane().add(panel);
              frame.setSize(new Dimension(200,100));
              frame.setVisible(true);
         public static void main (String [] args)
              TextFieldVerifier v = new TextFieldVerifier();
    }

    Thanks, JayDS! Your suggestion worked. And I've come up with the following code with help of your suggestion. However, I'm not happy with the fact that I see empty spaces in my JFormattedTextField when I don't have anything entered in it, or when my textfield has focus and I click it again. Also, I'd like the text alignment to always stay on the right, even if I have less than 3 digits.
    Could you please help me out with that?
    Thanks!
    import java.awt.Dimension;
    import java.text.ParseException;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.text.MaskFormatter;
    public class TextFieldVerifier {
         public TextFieldVerifier()
              JFrame frame = new JFrame("My TextVerifier");
              JPanel panel = new JPanel();
              JLabel firstTextFieldLabel = new JLabel("First: ");
              JLabel secondTextFieldLabel = new JLabel("Second: ");
              try{
                   VariableLengthMaskFormatter formatter = new VariableLengthMaskFormatter("###");
                   final JFormattedTextField myTextField = new JFormattedTextField(formatter);
                   myTextField.setHorizontalAlignment(JTextField.RIGHT);
                   myTextField.setPreferredSize(new Dimension(100,20));
                   panel.add(firstTextFieldLabel);
                   panel.add(myTextField);
              } catch (ParseException ex) {
                   ex.printStackTrace();
              JTextField secondTextField = new JTextField();
              secondTextField.setPreferredSize(new Dimension(100,20));
              panel.add(secondTextFieldLabel);
              panel.add(secondTextField);
              frame.getContentPane().add(panel);
              frame.setSize(new Dimension(200,100));
              frame.setVisible(true);
         protected MaskFormatter createFormatter(String s) {
             MaskFormatter formatter = null;
             try {
                  formatter = new MaskFormatter(s);
             } catch (java.text.ParseException exc) {
                 System.err.println("formatter is bad: " + exc.getMessage());
                 System.exit(-1);
             return formatter;
         public static void main (String [] args)
              TextFieldVerifier v = new TextFieldVerifier();
    }Edited by: programmer_girl on Mar 10, 2008 11:10 PM

  • How to get character entered in JPasswordField

    Hi,
    I am using a JPassword field and i need to get the password entered(i mean the characters entered)
    getText() is depricated so i used getPassword(), but it gave me some other things (%@$#) for what i entered (abcd).
    1) How can i get the exact character. Like if i type "abc" i want to get "abc".
    Thank you

    Yes, but I would think twice about storing the password in the database. That kind of defeats the purpose of having a password. Anyone with basic knowledge of SQL can find it.
    There are some tricky things you can do such as using a hash to make the password illegible in the database, then you would reverse the hash in your program when you read it back again. But even this is not real secure.

  • How to limit the number of boxes to tick in LC

    I would like to know the "javascript" in Lice Cycle to limit the number of boxes ticked
    for example i have 20 boxes and i would like that the client can only tick 5 boxes
    thank you

    Thank you Steeve
    But my problem is that i don't know which boxes the client will want to tick so all boxes have to be available at the beginning.
    When the client will have ticked 5 boxes in this case and only in this case, all others boxes must become unavailable so it is very difficult to set up
    if you have any idea
    thanks anyway
    Date: Fri, 15 May 2009 09:44:33 -0600
    From: [email protected]
    To: [email protected]
    Subject: How to limit the number of boxes to tick in LC
    This is a form validation issue. For example, you may have a text field and the value entered into that field determines which checkboxes must be completed to satisfy a validation or business rule. The attached sample requests the value "foobar" to be entered into a text field. If you enter "foobar" and exit the field, 2 of 3 checkboxes have the access property changed to "protected", meaning a user cannot no longer select those checkboxes.
    // form1.page1.subform1.input::exit - (JavaScript, client)
    if (this.rawValue == "foobar") {
        form1.page1.subform1.cb2.access = "protected";
        form1.page1.subform1.cb3.access = "protected";
    Steve
    >

  • Limit characters displayed

    Does anyone know how to limit the characters outputted from a database to a specific number such as 100?
    I want to have a preview window that only displays a bit of whats stored so the user can click "click here for more details" and read the full information.
    i'm using
    <% out.print(x_product_info); %>
    thanks.

    <% out.print(x_product_info.substring(0,100)); %>

  • Help!! I can't find how to get the enter key......

    Hi...
    I am information technology student. I am now first year second semester
    I need help because I don't know how to catch the enter key in JTextField.
    I want to submit the form when user press the enter key in JTextField....
    I only can find KeyEvent.VK_ENTER ( actually that is not what I want)
    Another thing is
    "How to add a JMenuItem into a JMenuItem...."
    I want to do like this.... When you click the "Tools bar" in View item.. another menu comes out...???"
    He he... I hope you all can understand what I mean.. because I know my english is not so good... because I am from Myanmar.
    for(int i=0;i<1;i--)
    System.out.println("Thanks A lot");
    Thu Rein Nyo...
    You also can reply by e-mail.. thanks

    Hi...
    I am information technology student. I am now first
    year second semester
    I need help because I don't know how to catch the
    enter key in JTextField.it can be done easily by adding an actionListener to the JTextField. So when u enter something in the field and press enter the event will be caught and the code which u write in that will get executed.
    Eg:
    JTextField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Your code comes here.
    Another thing is
    "How to add a JMenuItem into a JMenuItem...."
    I want to do like this.... When you click the "Tools
    bar" in View item.. another menu comes out...???"I think you are gettin confused with JMenu and JMenuItem. What u need to do is create a JMenu add another JMenu to it and add JMenuItems to it.. and ur problem will be solved.
    He he... I hope you all can understand what I mean..
    because I know my english is not so good... because I
    am from Myanmar.
    for(int i=0;i<1;i--)
    System.out.println("Thanks A lot");
    Thu Rein Nyo...
    You also can reply by e-mail.. thanks

  • How pass ext characters to a stored proc by odbc when enable sqlserver syntax is on??

    how pass french characters or extended characters to a stored procedure by odbc
    error: ORA-01756: quoted string not properly terminated
    une chaine entre apostrophhes ne se termine pas correctement
    oracle Retrieving extended characters thru ODBC
    PL/SQL procedure parameters
    hi, i hope you can help to me.
    I have a problem with french and german characters.
    i have a little stored procedure than return what i'm passing to him.
    see these example: (the second one work fine on plsql)
    first exemple:
    1) i created a new odbc dsn
    2) i'm going into sqlserver migration tab to choose
    Enable Exac Syntax.
    3) i'm open Winsql (this is a odbc tools)
    http://www.indus-soft.com/winsql/
    4) i'm write
    exec ksp_test 0,'HiLLO ORACLE'
    i receive this error:
    Error: ORA-01756: quoted string not properly terminated
    (State:S1000, Native Code: 6DC)
    I trying to changed too the NLS_LANG in the registry
    like FRENCH_CANADA.WE8ISO8859P1
    French_France.WE8ISO8859P1
    but without any success..
    i got the same problem with
    oracle 9 database with utf8 characters set.
    oracle 8.1.7 with iso8859p1 characters set.
    i trying all latest odbc driver from oracle website.
    second exemple:
    SQL> variable mytest refcursor;
    SQL> exec ksp_test (0,'HiLLO ORACLE',:MYTEST);
    PL/SQL procedure successfully completed.
    SQL> PRINT MYTEST;
    Your Database Value
    HiLLO ORACLE
    CREATE OR REPLACE PACKAGE KSP_PLSQLRSETPKG
    AS
    TYPE RCT1 IS REF CURSOR;
    END;
    CREATE OR REPLACE PROCEDURE KSP_TEST (
    PATCH INT DEFAULT 0,
    PONC VARCHAR2,
    RC1 IN OUT KSP_PLSQLRSETPkg.RCT1
    AS
    BEGIN
    OPEN RC1 FOR
    SELECT PONC "Your Database Value" FROM DUAL;
    FROM DUAL;
    RETURN ;
    END;
    i'm trying also different nls setting but no good result.
    AMERICAN_AMERICA.US7ASCII
    AMERICAN_AMERICA.WE8MSWIN1252
    FRENCH_CANADA.WE8DEC
    FRENCH_CANADA.UTF8
    FRENCH_CANADA.WE8MSWIN1252
    FRENCH_FRANCE.WE8DEC
    FRENCH_FRANCE.UTF8
    FRENCH_FRANCE.WE8MSWIN1252
    is working well on sqlplus but not by odbc..
    also..
    i'm declare a variable and
    i set
    v_variable := 'id'
    and the procedure return the good syntax...
    i think is a odbc driver problem....
    the driver don't want to accept a extended characters set by a parameters coming from the procedure.
    can you confirm to me ..this is a major bug for the driver..
    my procedure is very basic to make a little test.
    did you try my procedure to be sure you have the same problem?
    i try with a oracle instance utf8,WE8MSWIN1252 and
    i got always the same problem.
    if i write insert into test values ('di');
    everything is fine...but when i call the procedure...
    the procedure don't want to accept any german..french or any extended characters...
    our application is working by odbc driver.
    i'm pretty sure is a bug in the driver ...the bug is coming only when i select "ENABLE EXEC SYNTAX" IN THE DSN (SQLSERVER MIGRATION SECTION) ... i try with Shema Database and Owner and Empty and i got
    always the same problem
    exec KSP_TEST 0,'TiEST'
    ------------------------>>>>>>>NOT WORKING.
    BUT IF I WRITE
    CALL KSP_TEST (0,'TiEST')
    ------------------------->>>>IS WORKING
    if i select enable exec or i unselect enable exec...
    the CALL KSP_TEST...... is always working properly.
    BETWEEN THESE SYNTAX THE NLS_LANG IS NEVER CHANGED....
    IS WORKING.....THE NLS_LANG IS GOOD.......because i make a little modification in procedure to be sure the INSERT IS inside the database CORRECTLY.
    CREATE OR REPLACE PROCEDURE KSP_TEST
    PATCH INT,
    PONC VARCHAR2
    AS
    v_test varchar2(100);
    BEGIN
    v_test := 'test';
    INSERT INTO YYY VALUES (PONC);
    END;

    If  "just using Crystal Reports XI R2" means using Crystal Report Viewer and do not want to see the prompt, please follow the below steps.
    1. Select the report you want to see
    2. Select "Process" tab
    3. Select Parameters menu under the process tab.
    4. You would see two date parameters there.
    Select the [Empty] value for each parameter and fill out the value you want.
    Hope this would help.

  • How can i use RTFEditorKit for JTextField.

    hi all,
    how can i use RTFEditorKit for JTextField.
    thanks in advance
    daya

    Don't cross post. This is a Swing related question and you have already posted in the Swing forum:
    http://forum.java.sun.com/thread.jspa?threadID=619619&tstart=0

  • How to limit sharing apps to only two devices? because i have an iPod touch and an iPad. i just want to limit the sharing of apps to those two.

    how to limit sharing apps to only two devices? because i have an iPod touch and an iPad. i just want to limit the sharing of apps to those two. because my brother is using my apple id too on his ipod. i want to limit it to mine only. tnx!

    You can go into settings and turn sharing off in the programs on the device you don't want to share too.

  • How to limit the max dialog no that one user can use at the same time?

    Hi,
    I meet one performance problem that one user can open 6 sessions in the GUI and he/she can run 6 reports at the same time witch could occupy 6 dialogs in the sap R/3 instance. It makes poor performance for other users.
    Would you pls tell me how to limit the no. of sessions one user can create at the same time or how to limit the no. of dialogs one user can occupy at the same time?
    Thanks a lot!
    I used this parameters in the default profile as blew:
    rdisp/rfc_check 1
    rdisp/rfc_use_quotas 1
    rdisp/rfc_max_own_used_wp 20 (means: 20%)
    It still didn't work.
    Sean

    Hello,
    We can reserve DIA W.P by giving value to the parameter :- rdisp/rfc_min_wait_dia_wp=1(default)
    that have to necessarily remain free for other users.
    This parameter is used to reserve a number of dialog work processes for Dailog mode.
    For eg. If 10 dialog w.p. are configured for the instance(rdsip/wp_no_dia=10) and the parameter rdisp/rfc_min_wait_dia_wp=3 is set,parallel RFC's can occupy a maximum of 7 DIA W.P.3 DAI W.P. always remain free for dialog mode.
    But now the question is how we assign/restrict this free dialog w.p. to the specific user.
    Reply...
    Regards,
    JUNAID

  • I deleted my SMTP how can I re enter it?

    How can I re-enter a deleted SMTP outgoing mail server?

    What Are the Gmail SMTP Settings?
    Problems sending mail with POP or IMAP - Gmail Help - Google Help

  • Mountain Lion: on the Notification Center notification and Mail:  how to limit notifications to an email account?

    Mountain Lion: on the Notification Center notification and Mail:
    how to limit notifications to an email account?
    At least, is it possible to choose what email account, the arrival of a message launch a notification.
    When running more than a dozen e-mail accounts, it may be appropriate to display only the notifications of one or two major accounts.

    Which os are you using?  Which Mail version are you using? 

Maybe you are looking for

  • N8 music player problem

    When i open music player it always starts with the albums and i have to select songs all the time to show me all the songs. How do i do to make songs list open instead of albums everytime when i open the player?

  • Crystal Reports publication personalization with an OR statement

    Hello, I want to publish a crystal report but do not see a method to implement an OR statement like you can with interactive filters or query prompts. Currently any personalization's I make are all AND filter. My report when run on demand can bring b

  • Display my computer on tv

    display my computer on tv

  • Dual Display view under Windows XP

    Does my Notebook display dual view under Windows XP?

  • VF03 - Userexit to make column details invisible

    Dear techies, Following is my requirement in tcode VF03 & VF02: In line item details, i need to make data in column 'COST' invisible. I have gone through all userexits avaliable for tcode VF02 nad VF03. But none of them satisfies my requirement. Plea