Hello,
I've written a class which extends JTextField, basically for handling number input.
I'm having a bit of trouble getting one of the parts to work.
Code://text field's length is the maximum, character typed is a digit if(getText().length()==maxCharacters&&Character.isDigit(currentKey)) { //nothing inside the text field is selected if( getSelectedText().isEmpty()){ ev.consume(); return;} } // passed the tests, process key as normal else{ super.processKeyEvent(ev); }
Basically I want to consume the event if the max characters has been reached and there is no text selected, this is so it lets users overwrite the text by selecting the text and type a digit.
Regards
Kyle
Never mind I managed to get it to work. Basically I got rid of that section of code and added a class called JTextFieldLimit to do the work
The code for JTextFieldLimitCode:setDocument(new JTextFieldLimit(maxCharacters));
Code: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); } } }
hi i belive that you need to write your own overriding Exception as to intcept the system exception that your code is throwing.
this is a example of one for email
hope it helps
---------------------------- emailclass --------------------------------
-------------------------- the custom exception ---------------------------------------------------------Code:public class EmailAddress { private String domain = ""; // Domain component private String emailAddress; // Full email address private String server = ""; // Server component private String user = ""; // User component // Constructor default public EmailAddress() throws EmailException { this("user@server.com"); } // Constructor requires address public EmailAddress(String address) throws EmailException { int pos1, pos2; // Parse into components pos1 = address.indexOf('@'); if (pos1 == -1) { throw new EmailException(address, "Email address requires an @"); } this.user = address.substring(0, pos1); // Set the user if (pos1 == 0) { throw new EmailException(address, "Email address requires a username"); } pos2 = address.indexOf('.'); if (pos2 == -1 || (pos1+1) >= pos2) { throw new EmailException(address, "Email address requires a server name"); } this.server = address.substring(pos1 + 1, pos2); // Set the server this.domain = "com"; // Assumed for now this.emailAddress = address; // Set full email address } // Return email address string public String getAddress() { return this.emailAddress; } public String toString() { return this.getAddress() + "\n" + "user = " + this.user + "\nserver = " + this.server + "\ndomain = " + this.domain; } }
Code:public class EmailException extends Exception { private String invalidAddress; public EmailException(String invalidAddress, String message) { super(message); this.invalidAddress = invalidAddress; } public String getInvalidAddress() { return this.invalidAddress; } }
Last edited by Vswe; 03-20-2010 at 03:04 AM. Reason: Use Code tags(# button) when posting code
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks