This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JFormattedTextField with 6 integers and 2 decimals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFormattedTextField with 6 integers and 2 decimals" Watch "JFormattedTextField with 6 integers and 2 decimals" New topic
Author

JFormattedTextField with 6 integers and 2 decimals

Benz
Greenhorn

Joined: Aug 05, 2004
Posts: 2
Hi All,
I have a field in which the validation goes something of this sort i.e the user can enter at the max 6 integers and two decimal places. I have tried setting the format by using JFormattedTextField. To some extent i am able to do but when the user enters anything greater than 6 digits it is accepting.
It shudn't allow the user to type beyond 6 integers and 2 decimals.
ex:
123.23 ( fine )
1234567 (here i shouldn't allow the user to type 7).
please let me know if anyone of u have done.

Thanks in advance,
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
There may be multiple ways to do this, but I would recommend to use a document listener with a JTextField. For tutorial on document listener, here is a link:

How to write a DocumentListener

Also, the following tech-tip from Sun JDC may help you regarding this:

Validating Numerical Input in a JTextField

HTH.
praveen kumar gudapati
Greenhorn

Joined: Sep 26, 2003
Posts: 15
Use the DocumentFilter to filter inputs in JFormattedTextField.

DefaultFormatter formatter = new DefaultFormatter(){
protected DocumentFilter getDocumentFilter() {
return new DocumentFilter();
}
};

jFormattedTextField.setFormatterFactory(
new DefaultFormatterFactory(formatter));

So this is the way to actually set the Filter.
I have programmed a helper filter class which you can use, here goes the code:

package de.tourdata.utils.swing.text;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.awt.*;


public class DefaultDocumentFilter extends DocumentFilter {

public static final byte LOWERCASE = 1;
public static final byte UPPERCASE = 2;
public static final byte ALPHA = 3;
public static final byte NUMERIC = 4;
public static final byte FLOAT = 5;
public static final byte ALPHA_NUMERIC = 6;
public static final byte ALL_SIGNES = 7;
protected byte acceptedChars;
private int maxCharacters;
private String validCharacters;
private String inValidCharacters;

public DefaultDocumentFilter(int maxChars) {
this(ALL_SIGNES, maxChars);
}

public DefaultDocumentFilter(byte acceptedChars) {
this(acceptedChars, 32000);
}

public DefaultDocumentFilter(byte acceptedChars, int maxChars) {
this.acceptedChars = acceptedChars;
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs,
String str, AttributeSet a)
throws BadLocationException {
if (!isAcceptableAfterFiltering(str)) {
return;
}
// truncate the inserted string so the contents
//would be exactly maxCharacters in length.
if ((fb.getDocument().getLength() + str.length()) > maxCharacters) {
str = str.substring(0, (maxCharacters - fb.getDocument().getLength()));
}
str = applyInputmask(str);
super.insertString(fb, offs, str, a);
}

public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException {

if (!isAcceptableAfterFiltering(str)) {
return;
}
//truncate the replacement string so the contents
//would be exactly maxCharacters in length.
if ((fb.getDocument().getLength() + str.length() - length) > maxCharacters) {
str = str.substring(0, (maxCharacters - (fb.getDocument().getLength() - length)));
} else {
Toolkit.getDefaultToolkit().beep();
}
str = applyInputmask(str);
super.replace(fb, offs, length, str, a);
}


protected boolean isValidCharacter(char aChar) {
String filter = getValidCharacters();

if (filter != null && filter.indexOf(aChar) == -1) {
return false;
}
return true;
}

protected boolean isInvalidCharacter(char aChar) {
String filter = getInvalidCharacters();
if (filter != null && filter.indexOf(aChar) != -1) {
return true;
}
return false;
}

private String getInvalidCharacters() {
return inValidCharacters;
}

private String getValidCharacters() {
return validCharacters;
}

public void setValidCharacters(String validCharacters) {
this.validCharacters = validCharacters;
}

public void setInValidCharacters(String inValidCharacters) {
this.inValidCharacters = inValidCharacters;
}

private boolean isAcceptableAfterFiltering(String str) {
boolean accepted = false;
str = applyInputmask(str);

if (str == null)
return accepted;

if (acceptedChars == ALPHA) {
for (int i = 0; i < str.length(); i++) {
if ((Character.isLetter(str.charAt(i)) || isValidCharacter(str.charAt(i))) && !isInvalidCharacter(str.charAt(i))) {
accepted = true;
} else {
return false;
}
}
} else if (acceptedChars == NUMERIC) {
for (int i = 0; i < str.length(); i++) {
if ((Character.isDigit(str.charAt(i)) || isValidCharacter(str.charAt(i))) && !isInvalidCharacter(str.charAt(i))) {
accepted = true;
} else {
return false;
}
}
} else if (acceptedChars == FLOAT) {
for (int i = 0; i < str.length(); i++) {
if (((Character.isDigit(str.charAt(i))) || (str.charAt(i) == '.') || (str.charAt(i) == ',') || isValidCharacter(str.charAt(i)))
&& !isInvalidCharacter(str.charAt(i))) {
accepted = true;
} else {
return false;
}
}
} else if (acceptedChars == ALPHA_NUMERIC) {
for (int i = 0; i < str.length(); i++) {
if ((Character.isLetterOrDigit(str.charAt(i)) || isValidCharacter(str.charAt(i))) && !isInvalidCharacter(str.charAt(i))) {
accepted = true;
} else {
return false;
}
}
} else {
for (int i = 0; i < str.length(); i++) {
if (isValidCharacter(str.charAt(i)) && !isInvalidCharacter(str.charAt(i))) {
accepted = true;
} else {
return false;
}
}
}
return accepted;
}

private String applyInputmask(String str) {
if (str == null)
return null;

if (acceptedChars == UPPERCASE)
return str.toUpperCase();

else if (acceptedChars == LOWERCASE)
return str.toLowerCase();

return str;
}

}

//////////////////////////////////////////////////////////////
By using this filter you can even dictate which chars are not allowed and which are allowed.

Have fun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFormattedTextField with 6 integers and 2 decimals
 
Similar Threads
JSpinner keyboard input event
JFormattedTextField problem
Edit masks in text fields
Formatting of textfield
confused in converter