JTextField: delete last entered char

how can i delete the last entered char in a JTextField.
In my textfield a Person can only enter 5 Chars. If you enter mor than 5 chars, the chars behind the 5th position must be deletet an the caret must stay behind the 5th char.
How can i do this in java??
Java let always stay the last typed char behind my string, i cant remove it.
2.Question: with the method .getText() i get a String, but the last pressed key is not in that string
how can i make it right??

First Construct this method
     public static void setLenText(java.awt.event.KeyEvent event,int len){
     if (event.isConsumed())
return;
char c = event.getKeyChar();
JTextComponent source = (JTextComponent )event.getSource();
if (source.getText().length() >= len &&
event.getKeyChar() != event.VK_BACK_SPACE &&
source.getSelectionStart() == source.getSelectionEnd())
source.getToolkit().beep();
event.consume();
then in the KeyTyped event of the textfield:
void JTextField_keyTyped(java.awt.event.KeyEvent event)
          GlobalMgr.setLenText(event,5);
By

Similar Messages

  • How can I select the last 2 chars of a string

    Hello all,
    I am trying to select the last 2 characters of a string to match them to another string. This is for a Poem generator that contains 20 or so words and I have to pick 2 words at random, them look at the last 2 characters to see if they match in each string. Say "Plain" and "Rain" would match because they both have "in" at the end. The length of each word could vary. I don't want this doing for me just what Method should I be looking at.
    Thanks.

    hi,
    try this:
    public class StringTest {
         public static void main(String args[]) {
              StringTest obj = new StringTest();
              obj.same();
         void same() {
              String rain = new String("Rain");
              String plain = new String("Plain");
              int rainLength = rain.length();
              int plainLength = plain.length();
              if(rain.substring(rainLength-2).equals(plain.substring(plainLength-2))) {
                   System.out.println("same");
              else {
                   System.out.println("different");
    }you can use the length method to get the length of a string and then use the substring method, passing it length-2, so get the last 2 chars.

  • I need to reprogram the delete and enter key to the left side of a external keyboard for the ipad mini. don't need the caps/lock or ctrl key and due to an injury my daughter can only use her left hand to type. She is using a text to speech app to verbaliz

    I need to reprogram the delete and enter key to the left side of a external keyboard for the ipad mini. I don't need the caps/lock or ctrl key. Due to a brain injury my daughter can only use her left hand to type. She also uses a text to speech app to verbalize all of her needs since her speech isn't intelligible any longer either. And her vision was significantly affected also, so the keyboard has to be mounted about 6 inches from her face. So to reach across the keyboard with her left hand to the right side delete and enter button is physically difficult and causes typing errors, which cause people to not understand what shes trying to say.
    The best keyboard so far is the Zagg folio mini. I just had to make stickers to enlarge the letters on the key buttons.
    Does anyone know how I can reprogram these two keys? Or where I can buy a wireless mini keyboard for Ipad made for lefthanders with these two functions on the left side. I have searched for days and days. It's sooooo important to me that she be able to contribute her voice again. Imagine if you got in a car accident and couldn't speak clearly any longer, but understood everything still. Thanks for any help and suggestions you all take the time to share with me. I really appreciate the kindness of strangers to help me help my daughter.
    Sami's mom

    Sami\'s mom wrote:
    I need to reprogram the delete and enter key to the left side of a external keyboard for the ipad mini.
    You cannot.

  • Pick last 2 char of a field using rule file

    Hi
    I may be missing out anything but i need some help on this.
    I have column with city and state : city<space>State
    the state is 2 character while city name is of varied length starting from two to ten characters.
    Now i need to pick up last two characters of state using rule file.
    example of data:
    Glendale WI
    NA NJ
    New York NY

    Check this i guess this is answered by one the Oracle Guru
    Pick last 2 char of a field using rule file
    Thanks,
    KKT

  • Delete last row in database?

    How do I delete last row in mysql database? I tried like this without luck:
         public void minus()
         int tal=0;
         try{
         ResultSet row = db.select("select * from midlertidigregning order by id asc");
         row.last();
         tal = row.getInt("nr");
         }catch( SQLException t ){}
              db.insert("delete from midlertidigregning where nr = '"+tal+"' limit 1");
         }

    What didn't work? Did something fail, or did the row not get deleted as expected? Did you issue a commit after the delete?

  • Confirmations- (check last entered % and input on the same screen)

    wanted to check if there's any transaction where you can check last entered degree of processing for confirmation and enter the current degree of processing/% of completion on the same screen.
    tried CN48N but it isn't allowing to enter confirmations..

    Hi
    Execume me Moderator for posting for closed thread.
    Wanted to correct or update my posting.
    You can also use CN48N with fileds Counter, Degree of procssing so that it will let you know the percentage of completion.
    Regards,
    PSR

  • How do i get the last 4 chars

    String FileE = myFile.getName();
    This gives me a file name but how do I a reverse trim and get the last 4 chars??

    Hi
    What are you trying to achieve here, why are you pulling a file into a String Buffer ?
    fileName.getName() will output a string, you can use the following statement to get the last four characters of the filename
    Here is an example
    String fileName = "testfile";
    String lastFourChars = fileName.substring(fileName.length()-4);
    This will give you the last four characters of the filename.
    But if you are trying to get the extension of the file
    Do this
    String extension = fileName.substring(fileName.lastIndexOf("."));
    Thanks
    Swaraj
    Thanks
    Swaraj

  • I try to delete last backup but witn no success

    I try to delete last backup but witn no success

    To delete a device backup you need to do it on your computer and not on your device because the backup is saved on the computer not the device

  • Remove last 3 char ..

    Hi ,
         I need to remove last 3 char from my varible which is char 30.
    For example : WERKS = 1000 and .
    I need to remove last 3 char (and) from the above varibale .Kindly guide this
    Regards,
    VC

    Hi veerachamy,
    Just copy this code..it will work fine even if the field changes dynamically..
    I took w_char as 30 char..u can give ur field name there...
    data:
    w_char(30) type c value 'asdfghjklpoiu',
    w_tmp(30) type c,
    w_tmp1 type i.
    w_tmp1 = strlen( w_char ).
    w_tmp1 = w_tmp1 - 3.
    w_tmp = w_char+0(w_tmp1).
    write: w_tmp.
    Hope it resolves the issue...
    Regards
    Kiran

  • How to delete last user session from backend SQL query?

    Sometimes user session gets closed between some work by any reason.
    When next time that user logs to P6, he gets some error or particular functionaly dosen't work for that user or system hangs.
    In that case if i decided to clear last session history of that user from database, how to write SQL query?
    I don't want to delete all user sessions from table USESSION and USESSAUD, i simply want to delete last user session of particular user (E.g XYZ).

    I am quoting this from MOS Doc Id "Why would multiple session records be present in the User Sessions screen in P6 Web, and why might some of them have different IP addresses? (Doc ID 1600172.1)"
    Multiple sessions show up for users since different sections of P6 Web have their own sessions associated with them. If a user is authorized to use multiple areas of the software they will have multiple sessions each time they log in. Additionally, if users are closing their browsers before logging out of P6 Web Access you might see some past sessions still appearing in the list. These will eventually be cleared out by background jobs, however you can also reset the sessions in the software by clicking the "Reset User" link (Administer > User Sessions > Manage User Sessions), or by choosing the "Reset All Users" link (Administer > User Sessions > Manage User Sessions) to do this for all past sessions.
    Multiple IP addresses for sessions can happen when a user logs in from different machines. For example, a person may login at their desk, but then go to a colleagues workstation to discuss a project, and log in from there. Doing so will leave them with multiple IP addresses in the session records.
    Hope this helps
    Regards,
    Sachin Gupta

  • How to take last 5 char from a string

    Hi All
    I have a requirement,
    base on some comp i have to populate another field.
    in this case i have compare last 3 char of a string 
    example if total string say asasdffrx i have to take last three char here it is frx.
    always i have to last 3 char.
    i can use +6(3) but it wont work every time because string lenght is not fix for all vaue.
    thanks

    Hi,
    A.H.P sample is ok but insert additional check for strigs shorter than 3 (to avoid negative offset):
    data : n TYPE I.
    n = STRLEN( zfield ) - 3.
    if n >= 0.
    result = zfield+n(3).
    else.
    result = zfield.
    endif.
    Krzys

  • How to delete last achievement on game center?

    hallo,
    may i ask you?
    i have problem with my last game level on my iphone
    can i delete last achievement in game center,
    please help,this game is important for me, if i dont delete the last achievent,i can't continue my last level,and will start from first level

    i have iOS 5.0.1, you have to go to game center and then to "me", it will say something like
    "i am (nickname)... (status)" next to it you will see a square, wich is the profile picture, tap on it and it will ask you to select a photo or take a photo.
    hope this helps

  • How do you restrict new line in input field or delete last char. in as3?

    Sorry, Noobie here!
    I have a text input box. If the enter key is pressed, a new line begins; thus making the previous line invisible unless the backspace key is pressed.
    So, if i type "1" into the input box, then hit eneter the number "1" becomes invisible, unless I hit back.
    Now, I still need the enter key because i have an event listener that calls a function once it is pressed, but i still need that "1" to be visible.
    So, I need one of either:
    To be able to restrict the input text field from creating a new line (making max line allowed= 1)
    or
    To delete the last character entered in the input text if the enter key is pressed.
    Both of these should have the same result, however I don't know how to do both.
    If anyone can help, i'd be very grateful!

    This worked! Thanks so much!

  • Deleting last char in a string...

    I have a string that is created as follows: strBuffer += k.getKeyChar();(k is KeyPressed ...KeyEvent) How can I delete the last Char in the string strBuffer?

    Hi
    // simply substring
    if (strBuffer.length() > 1)
        strBuffer = strBuffer.substring(0, strBuffer.length()-1);
    // you may also use
    if (strBuffer.length() > 1)
        StringBuffer buffer = new StringBuffer(strBuffer).
                                   deleteCharAt(strBuffer.length()-1);
        strBuffer = buffer.toString();
    }I've not tested the above code myself & I've written the above code on the go after reading your query. The above code definitely work & help you.
    Regards,
    JPassion

  • Delete last char from a console window.

    Hey all, could be a silly question.......
    Is it possible to delete a char from a console window?
    e.g.
                     System.out.print("-");
                   System.out.print("|");
                   System.out.print("/");
                   System.out.print("\\");I'm trying to give the illusion of the line spinning, but cant delete the last printed character. I've tried adding "\b" but with no joy.
    And help would be nice, as it's the little things that always take the longest!
    T.

    @Op. You can use JCurses if you accept that your code might not execute on all platforms.
    http://sourceforge.net/projects/javacurses/

Maybe you are looking for