How to stop typing on JTextField at...

Hello, I have JTextField and want to limit inputting after
certain number of text. Say, I want to limit after
10 characters are inputted. How to do this?
I try setColumns method but does not give the effect
I want.
Anybody knows please post!
Thanks,
Ted.

Here's an simple and easy solution using PlainDocument. It gives a beep sound if the user tries to enter text after the specified length.
textField.setDocuemnt( new TextFieldVerifier() );
class TextFieldVerifier extends PlainDocument {
int requiredLength = 10; // whatever length you want
public void insertString( int offset, String str, AttributeSet attSet ) throws BadLocationException {
boolean valid = true;
if (str == null) {
return;
String old = getText( 0, getLength() );
/* insert the new string at the given offset, into the old string */
String newStr = old.substring( 0, offset ) + str + old.substring( offset );
if (newStr.length() > requiredLength)
valid = false;
Toolkit.getDefaultToolkit().beep();
if ( valid )
super.insertString( offset, str, attSet );
}

Similar Messages

  • How to stop size of JTextField from changing when I change font to bold

    I'm working on a Sudoku solver for a CS project. I've got JTextFields for the input. I want to bold the entries of the begining puzzle, like they are in the magazines. When I set the font to bold, it changes the size of the text field. I'm using the gridbaglayout and have tried everything to set in stone the size of a text field, but nothing seems to work. I've set the maximum, minimum size, the setSize method, as well as setting the prefered size. But no matter, when I change the font from plain to bold, the box grows in size. How do I prevent this from happening. I can post my gui code if it would help.
    Here is a link to my grid, with the upper left square having bold text:
    http://12.227.129.113/CS486/Project4/Project4.html

    add them to panel/s set as a gridlayout 9 x 9, or (3 x 3) x (3 x 3)
    simple demo of a 9 x 9
    import java.awt.*;
    import javax.swing.*;
    class Testing extends JFrame
      JTextField[] tf = new JTextField[81];
      public Testing()
        setLocation(200,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridLayout(9,9,5,5));
        for(int x = 0, y = tf.length; x < y; x++)
          tf[x] = new JTextField(2);
          tf[x].setHorizontalAlignment(JTextField.CENTER);
          p.add(tf[x]);
        tf[0].setFont(tf[0].getFont().deriveFont(Font.BOLD));
        tf[0].setText("9");
        tf[1].setText("3");
        tf[66].setFont(tf[66].getFont().deriveFont(Font.BOLD));
        tf[66].setText("9");
        getContentPane().add(p);
        pack();
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • How to call a method in jsf after stop typing in test box?

    Hi all,
    In my jsf page i have a text box in which user type some text field like "abc". i want after stop the typing i want a method to be called automatically and a datatable will be populated sith some result. my problem is is how to call the method automatically so that the user does not have to click any button after typing in the text box? plz help..
    Edited by: sailajoy on Dec 23, 2008 3:36 PM

    If it is after certain characters that the user types you can ascertain that the entry is done you can use onkeyup and within the javascript count the characters an when the necessary characters are set do the action you need to do.
    There should be some way by which you can suggest that the user has stopped typing i.e. either the max length has reached or the user has hit tab or went to another field. Going on other field too you can bring the focus back to your original field from where the user had hit tab. If you are thinking of something liek google suggest then AJAX is the best way to implement it.

  • HT204389 Can any tell me how i stop siri sayin the words i am typing when typing a message??

    Can any tell me how i stop siri sayin the words i am typing when typing a message??

    Hellooo Frndz....:)  i have to use UISplitviewController.and I want to do add UISplitViewController To UINavigationController but problem is that i can't do this...:(   i use MGSplitViewController Bt I Dont know Perfectly Use Of MGSplitViewController...
    so , plz give me answer for how to add UISplitViewController to UINavigationController ???
    --THNKS>>

  • I find very annoying that, although the cursor is flashing in the serch box, unless that box has been clicked in to enable it, typing goes to the nearest topic in the bookmark list. how to stop it from happening?

    i use Google home page as my firefox home page. often i find that although the cursor is flashing in the serch box, unless that box has been clicked in to enable it, typing goes to the nearest topic in the bookmark list instead of accepting the typing. how to stop it from happening? why not stop the cursor from appearing in the search box unless you have clicked in it?

    Do you organise your email into separate folders, or do you just let it accumulate in the Inbox?
    I would strongly advise you not to let it sit in the Inbox.
    When you see this happening, try right-click on the affected folder, select '''Properties''' then '''Repair Folder'''.
    Large folders (actually, a folder in Thunderbird is stored as a file, so it is sensitive to file size limits set by the underlying file system) can be problematic. This is one reason not to let messages collect in one folder. I use Thunderbird's Archive folders so that accumulated mails over several years can happily coexist because they are effectively stored in many small folders. No single mail store folder then exceeds the 2GB or 4GB sizes that have been known to stress the OS. I appreciate that current builds of Thunderbird and a modern 64-bit OS should be able to cope, but practically I find it slows down when given huge files, so I err towards a pragmatic solution; a large number of not very big files.
    Archives are searchable, and a Saved Search folder can give you a virtual composite folder allowing access to the entire Archive.

  • How to stop iphone from completing text before i finish typing

    how to stop iphone from completing text before i finish typing?

    I'm not sure what you are asking here. Can you describe your problem in a little more detail?

  • How to stop syncing the photos only on facebook

    how to stop syncing the photos only on facebook

    corg2333 wrote:
    yeaa but it is not only on facebook i think..
    Disable the automatic spell checking service in your browser.
    Safari
    --> Edit --> spelling and grammar --> uncheck "check while typing"
    Firefox
    http://support.mozilla.com/de/questions/500215

  • How to stop text box on iWeb page from automatically reverting to hyperlink when I click 'save'

    As far as I see, the main text box in the body section of my iWeb page reverts to a hyperlink (connecting to a file) when I click save.  I don't know how the hyperlink got activated to begin with--I must have done something, but I don't know what it was.  I tried this many times to fix it: went to Inspector "hyperlink" section, activated the text box in the page, un-clicked "make hyperlinks active" (so they are inactive), then un-clicked "Enable as a hyperlink."  That much works--the hyperlink (little white arrow in blue circle) disappears from the lower right corner of text box.  But as soon as I save (command S), the text box reverts to a hyperlink. 
    If I don't click save and move around the pages, it does not revert to a hyperlink.  It only reverts to a hyperlink when the file is saved.  Why is it doing this and how to stop this from happening--how to permanently return the text box to a non-hyperlink?
    The website page is: http://www.usronline.net/USR/VedicResearch.html.  If you click anywhere randomly in the main text, you'll get a prompt to download a PDF file.  This should not be, but I can't get this hyperlink to go away for good.
    Many thanks in advance.
    Vishnupriya

    Please pardon the reply delay--a big project deadline had to be met.  Now back to this work:
    Yes, the text in the oval object had a drop shadow (the oval object itself did not).  I removed the drop shadow from the text, but the image file icon stayed in the right corner of the oval object.  I deleted the text and then the image file icon disappeared.  But as soon as I typed text again inside the oval object (not even pasting the previous text) the image file icon appeared again.  I deleted the oval object entirely and simply typed the text inside the main text box itself.  The image file icon that was attached to that oval object is gone (along with the oval object).
    Still the image file icon is attached to the main text box, though.  I again tried to uncheck the Enable as hyperlink (same process as described in first posting) with that oval object gone, but when I "save" again it reverts to a hyperlink. 
    Next I tried this: I copied the text from the problem text box and pasted it into another text box (outside of the problem one).  Still in the new text box with the pasted text, the image file icon shows in the upper right corner. 
    I also removed all the drop shadow features from this main text box (there was one more, on some words typed in the main text box), but it is still showing as an image.
    I don't know how it became an image file, and I'm still stumped as to how to fix it (short of forgetting about trying to figure out how it became an image and just retyping all text into a new text box).
    The link you provided for "Web Safe Fonts" is returning this: "The server at www.ampsoft.net is taking too long to respond."  I'm using Arial and Georgia (Georgia only on the menu and header text) in the site, and I had verified before designing the site that these are web safe, so I had thought they were.
    If you or anyone have any further suggestions or suspicions on this matter, I'd be glad to hear them.
    Thank you.

  • How to stop multiple auto-switching to address bar every time I open a new tab and try to type something anywhere outside of address bar?

    How to stop multiple auto-switching to address bar every time I open a new tab and try to type something anywhere outside of address bar? Like something just wants me to use that embedded search when u type something not-web-address in address bar and hit enter. And the most ridiculous thing is that it happens repeatedly on like every second, like I just move down from address bar and start typing again, but then again it switches me to address bar, and 3, 4 times like that. And the result is also that I can't see the address of that page.
    I think its has something to do with my AVG antivirus, because this started the same time some AVG Nation started to appear in every new tab i open (and thats also irritating me, I read about it here on support.mozilla.org and it seems that the only solution is to completely reinstall Firefox, but I dont want to lose all my settings) but when i type something in address bar and hit enter it opens the search results in Google.
    Please try to help me, I like Firefox but I must switch to Chrome until I fix this problem.
    Thanks in advance

    First, please update to Firefox 32. 22 is no longer support nor is it secure. Then let us know if you still have this problem. [[Update Firefox to the latest version]]

  • When i type any letter on web search, earlier address appear. how to stop that?

    when i open firefox there is an item on toolbar ie web search and before that there is some space in which one type any item to search on web. once that is finished and again when some other item is to be typed to search on web, we start with the first letter of that item or subject so to say. as soon as that that letter is typed, down below appears the the entire list of the items previously searched.how to stop appearing of earlier searched items?

    You can do this by disabling form history.
    # In the Tools menu select Options to open the Options dialog
    # Select the Privacy panel
    # Set "Firefox will" to "Use custom settings for history"
    # De-select the setting "Remember search and form history"

  • How to stop the Debug-master-Beacon command from console

    Q: How to stop the Debug-master-Beacon command from console
    A:  
    -"Debug-master-beacon" This command is basically used to see the  master beacon  sent and the received .
    -This command will  help in figuring out master discover issues.
     -You can run this command only from the console of the Ap.
    - Once you run the command you can stop it only by typing the command
    "No debug-master-beacon"
    Sample command below

    Hi Shravanthi,
    How the Substitution will work for stopping the user from changing the Depreciation key in asset master data. Can you please ellaborate in details.
    Regards,
    BABA

  • My iPad 2 only keeps a set number of emails for me to view eventually deleting from the screen. How do stop this as it syncs with my phone and mac book pro neither of which do this?

    My iPad 2 only keeps a set number of emails for me to view eventually deleting from the screen. How do stop this as it syncs with my phone and mac book pro neither of which do this?

    Thanks for that. Much more constructive than the last comment. It's only the restriction code I can't recall, not the access passcode. So I can currently access the device, just not age restricted content. Does that's make a difference? I also wondered if anyone knew how many attempts you get to try to get it right. Now tried 21 times and so far nothing bad has happened but I am concerned I'll eventually be completely locked out of the device. That doesn't seem in the spirit of things though. Surely it's foreseeable that a child could repeatedly try to guess the code so I can't see that it would be right to lock the device down completely in that circumstance, particularly if the access code is being typed in correctly every time.
    Thanks

  • Detect user stopped typing

    Hi
    How can I detect if a user stopped typing for about 2
    seconds?
    After those 2 seconds I want a mc to start playing.
    tx
    Tom

    When they start typing start setTimeout for 2 seconds that
    will call a function that starts your clip playing.
    Each time they press a key restart the timeout. Something
    like this maybe? Assuming you are using a textbox and you don't
    want the time to start until they have entered their first
    letter.

  • Notes keeps '...updating'/syncing which stops typing

    It's fine that Notes updates and syncs when you first open it.
    But how can you slow the updating time, or wait until you leave the Notes pages, eg. command tab to switch applications.
    Currently, when typing a note, Notes updates while typing. Which stops typing and removes the insertion cursor to prevent further typing. Very annoying.
    Is there a way to slow the updating period to sy 5 mins.

    https://discussions.apple.com/message/19027602#19027602

  • How to stop autofillin?

    When writing the program suggests a result. How to stop This function?

    Is this with forms on web pages or typing an URL in the location bar?
    See also "Prevent Firefox from automatically completing URLs":
    *https://support.mozilla.org/kb/search-your-bookmarks-history-and-tabs-awesome-bar
    Use these steps to remove saved (form) data from a drop down list:
    #Click the (empty) input field on the web page to open the drop down list
    #Highlight an entry in the drop down list
    #Press the Delete key (on Mac: Shift+Delete) to remove it.
    *http://kb.mozillazine.org/Deleting_autocomplete_entries

Maybe you are looking for