Input a number in format
I try to create a PlainDocument for input a value range from 0.0 to 99999.99
i have not use DecimalFormat, because the user can type other things (i.e. 345..34 , 345a33.3)
if using MaskFormatter, it will force to user to fill in all the blank .
so, my purpose is that just allow user to input a number and one decimal point.
Is there other method can I do?
there is my code:
class PD_Double extends PlainDocument {
int MaxLength;
int IntLen=5,DecLen=2;
int DecPt=0,NumLen=0;
public PD_Double(int parMaxLength){
MaxLength=parMaxLength;
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException {
System.out.println("offset:"+offset);
if (str == null) return;
try {
if (!str.equals(".")) {
double n = Double.parseDouble(str);
}
int LeftIndex =getText(0,getLength()).indexOf(".");
int RightIndex=getText(0,getLength()).lastIndexOf(".");
if ((LeftIndex < 0 & RightIndex < 0) || (LeftIndex == RightIndex) & !str.equals(".")) {
boolean cont=!false;
if (DecPt == 0) {
}
else if (DecPt == 1) {
if (getLength() < 3) {
cont=true;
System.out.println("@1");
}
else {
if (DecPt <= IntLen+1 & getLength()-DecPt < DecLen){
cont=true;
}
else {
cont=false;
if (DecPt <= IntLen+1 & getLength()-DecPt == DecLen) {
if (offset < DecPt)
cont=true;
else
cont=false;
}
}
}
}
else if (DecPt == 2) {
if (getLength() < 4){
cont=true;
}
else cont=false;
}
else if (DecPt == 3) {
if (getLength() < 5){
cont=true;
}
else cont=false;
}
else if (DecPt == 4) {
if (getLength() < 6){
cont=true;
}
else cont=false;
}
else if (DecPt == 5) {
if (getLength() < 7){
cont=true;
}
else cont=false;
}
else if (DecPt == 6) {
if (getLength() < 8){
cont=true;
}
else cont=false;
}
if ((getLength() + str.length()) <= MaxLength & cont) {
super.insertString(offset, str, attr);
NumLen=getLength();
}
}
}
catch (Exception ex) {}
}
protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng) {
super.removeUpdate(chng);
try { DecPt=getText(0,getLength()).indexOf(".")+1;
}
catch (Exception ex) {}
}
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng,AttributeSet attr) {
super.insertUpdate(chng,attr);
try { DecPt=getText(0,getLength()).indexOf(".")+1;
}
catch (Exception ex) {}
}
}
[2887 byte] By [
oliverwga] at [2007-11-27 2:06:52]

# 1
sorry, I have not read your code. use codetags next time you post code (see formatting tips: http://forum.java.sun.com/help.jspa?sec=formatting).
but if you are talking about JTextFields I think you might want to use a floatdocument.
save this class as FloatDocument.java:
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class FloatDocument extends PlainDocument {
private int m_nMaxChars;
private float m_fMinValue;
private float m_fMaxValue;
private boolean m_boMaxCharsSet = false;
private boolean m_boMinMaxValuesSet = false;
public FloatDocument() {
super();
}
public FloatDocument(int p_nNumChars) {
super();
m_nMaxChars = p_nNumChars;
m_boMaxCharsSet = true;
}
public FloatDocument(float p_fMinValue, float p_fMaxValue) {
super();
m_fMinValue = p_fMinValue;
m_fMaxValue = p_fMaxValue;
m_boMinMaxValuesSet = true;
}
public FloatDocument(float p_fMinValue, float p_fMaxValue, int p_nNumChars) {
super();
m_fMinValue = p_fMinValue;
m_fMaxValue = p_fMaxValue;
m_boMinMaxValuesSet = true;
m_nMaxChars = p_nNumChars;
m_boMaxCharsSet = true;
}
public FloatDocument(Document p_oDocument, int p_nNumChars) {
super();
// Below only invoked if constructor IntegerDocument(int p_nNumChars) has been used
if (m_boMaxCharsSet) {
m_nMaxChars = p_nNumChars;
try {
// need to move text from old document to new document
String l_strText = p_oDocument.getText(0, p_oDocument.getLength());
if (l_strText.length() > m_nMaxChars)
{
l_strText = l_strText.substring(0, m_nMaxChars);
}
insertString(0, l_strText, null);
}
catch (BadLocationException ex) {
}
}
}
public void insertString(int p_nOffset, String p_strString, AttributeSet p_oAttributeSet) throws BadLocationException {
if (p_strString == null) {
return;
}
else {
if (m_boMaxCharsSet) {
if ((getLength() + p_strString.length()) > m_nMaxChars) {
return;
}
}
String l_strNewValue;
int l_nLength = getLength();
if (l_nLength == 0) {
l_strNewValue = p_strString;
}
else {
String l_strCurrentContent = getText(0, l_nLength);
StringBuffer l_strCurrentBuffer = new StringBuffer(l_strCurrentContent);
l_strCurrentBuffer.insert(p_nOffset, p_strString);
l_strNewValue = l_strCurrentBuffer.toString();
}
try {
float l_fNewFloatValue = Float.parseFloat(l_strNewValue);
if (m_boMinMaxValuesSet) {
if (l_fNewFloatValue < m_fMinValue || l_fNewFloatValue > m_fMaxValue) {
return;
}
}
if (p_strString.equals(".")) {
super.remove(0,l_nLength);
super.insertString(0, l_strNewValue,p_oAttributeSet);
return;
}
float currentFloat = 0;
if (l_nLength > 0) {
String l_strCurrentFloat = getText(0, l_nLength);
if (!l_strCurrentFloat.equals("-")) {
currentFloat = Float.parseFloat(l_strCurrentFloat);
}
}
else {
currentFloat = 0;
}
if (currentFloat == 0 && l_fNewFloatValue == 0 && l_nLength == 1) {
return;
}
super.remove(0,l_nLength);
super.insertString(0, l_strNewValue,p_oAttributeSet);
return;
} catch (NumberFormatException exception) {
if (!l_strNewValue.equals("-")) {
return; //doesn't return on a single '-'
}
}
super.insertString(p_nOffset, p_strString,p_oAttributeSet);
}
}
}
Use it like this where you make your JTextField:
JTextField m_oFloatTextField = new JTextField();
m_oFloatTextField.setDocument(new IntegerDocument(0.0, 99999.99);
# 3
In particular, pretty easy with the [url http://java.sun.com/products/jfc/tsc/articles/reftf/]RegexFormatter[/url].
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
public class RegExExample {
public static void main( String[] args ) {
Runnable doRun = new Runnable() { public void run() { new RegExExample(); } };
SwingUtilities.invokeLater( doRun );
}
public RegExExample() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
RegexFormatter fmt = new RegexFormatter( "^(0|([1-9]\\d{0,4})?)((?:\\d)\\.[0-9]{0,2})?$" );
JFormattedTextField field = new JFormattedTextField( new DefaultFormatterFactory( fmt ) );
frame.getContentPane().add( field );
frame.pack();
frame.setVisible( true );
}
}