|
Hi All, I'm trying to create my own numeric textfields: I've created a subclass of JTextField and I'm using the KeyPressed method to obtain control over digits and separators entered. Is there a way to obtain the position of the cursor in the field when generating each KeyEvent? when using the following code: JRNumField jrn_myfield=new JRNumField(6,2, '.'); // creates a numeric textfield with 6 positions and 2 decimals and '.' as separator following valid values can be entered: 1234.56 12.56 1234 ... but also: 12.234 12221.1 To resolve this problem I need a way to obtain the position of the cursor (before or after the separator) Has anybody an idea how to do this or are there some 'home-made' components already available with the the same functionality? the code I'm using in keyPressed follows: /** * This method was created in VisualAge. * @param ke java.awt.event.KeyEvent */ public void keyTyped(KeyEvent ke) { // getLength() returns total number of digits // getDecimals() returns the digits after the separator // getNotation returns the type of separator (comma or period) char typedChar=ke.getKeyChar(); int length= getPositions(); // calculate the position of the separator int pos=getText().indexOf(getNotation()); // set separator to true if separator has allready been entered boolean separator=pos > -1; System.out.println("Keyevent=" + ke); // calculate the maximum total length in characters // if decimals specified add 1 to total length(separator) if (getDecimals() > 0) { length++; } // logging System.out.println("Event = " + ke + getText()); // if length of field is equal to maximum length-1 // consume the KeyEvent: throw it away! if (getText().length() > length-1){ if (getSelectedText().compareTo(getText())==0) { setText(""); return; } System.out.println("String to long"); ke.consume(); return; } // test the character being entered // is character a digit? else if (Character.isDigit(typedChar)) { System.out.println("It is a digit!"); // if decimals specified and seperator has been found if (getDecimals() > 0 && separator) { int numBefore=pos; int numAfter=getText().length()- (pos+1); // is number of digits before or after seperator reached? // logging System.out.println("Length= " + getText().length() + " Position= " + pos + " before="+ numBefore + " numAfter=" + numAfter + " decimals=" +getDecimals()); if (numBefore <= getPositions()-getDecimals() && numAfter <= getDecimals()) { return; // !! not correct !! // !! you need to know the position where the character has been entered... // !! (where the KeyEvent is being generated) } else { ke.consume(); return; } } else if (getDecimals() > 0 && !separator) { // if decimals specified and no seperator has been found if (getText().length() > getPositions()-getDecimals()-1) { ke.consume(); return; } return; } } // is character equal to separator? else if (ke.getKeyChar()== getNotation() && getDecimals() > 0){ System.out.println("It is a seperator!"); // if seperator has allready been used? if (separator) { System.out.println("Separator has already been used"); ke.consume(); } else { System.out.println("This is the separator!"); return; } } // no valid character entered // throw away event else { System.out.println("No seperator and no digit!"); ke.consume(); return; } } ---------------------------------------------------------------------------- ---------- Geert Van Landeghem Reynders Etiketten gvl@reynderseti.be (Office) hydra@vt4.net (Home) 03/460.12.56 (Office) 0477/759.533 (GSM) <<never play leapfrog with a unicorn>> ---------------------------------------------------------------------------- ---------- +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.