Hallo,
bin gerade dabei folgenden Quellcode so um zu schreiben, dass auch Double Werte akzeptiert werden.
"." und "," wird zwar akzeptiert, aber ich kann danach keine weiteren Zahlen setzen..
Zur Zeit ist also nur folgendes Möglich: 2423.(und auch noch 2423.......... oder 2423,,,,,,,,,,,,,)^^
hier der Quellcode:
bin gerade dabei folgenden Quellcode so um zu schreiben, dass auch Double Werte akzeptiert werden.
"." und "," wird zwar akzeptiert, aber ich kann danach keine weiteren Zahlen setzen..
Zur Zeit ist also nur folgendes Möglich: 2423.(und auch noch 2423.......... oder 2423,,,,,,,,,,,,,)^^
hier der Quellcode:
Code:
package com.siemag.common.utilities;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class IntegerTextField extends JTextField {
/**
*
*/
private static final long serialVersionUID = 1L;
private long maxValue = Long.MAX_VALUE;
private long minValue = 0;
private int maxLength = String.valueOf(maxValue).length();
private boolean isIPField = false;
/**
* Default constructor for IntegerTextField.
*/
public IntegerTextField() {
super();
}
protected Document createDefaultModel() {
return new IntegerDocument();
}
public void setMinValue(long value) {
minValue = value;
}
public long getMinValue() {
return minValue;
}
public void setIPField(boolean value) {
isIPField = value;
}
public boolean getIPField() {
return isIPField;
}
public void setMaxValue(long value) {
maxValue = value;
}
public long getMaxValue() {
return maxValue;
}
public void setMaxLength(int value) {
maxLength = value;
}
public int getMaxLength() {
return maxLength;
}
private class IntegerDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
long typedValue = -1;
StringBuffer textBuffer = new StringBuffer(IntegerTextField.this
.getText().trim());
//The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer
if ((offs >= 0) && (offs <= textBuffer.length())) {
textBuffer.insert(offs, str);
String textValue = textBuffer.toString();
if (textBuffer.length() > maxLength) {
JOptionPane.showMessageDialog(IntegerTextField.this,
"The number of characters must be less than or equal to "
+ getMaxLength(), "Error Message", JOptionPane.ERROR_MESSAGE);
return;
}
if ((textValue == null) || (textValue.equals(""))) {
remove(0, getLength());
super.insertString(0, "", null);
return;
}
if (textValue.equals("-") && minValue < 0) {
super.insertString(offs, new String(str), a);
return;
}
if (str.equals(".") || str.equals(",") ) {
// && isIPField
super.insertString(offs, new String(str), a);
return;
} else {
try {
typedValue = Long.parseLong(textValue);
if ((typedValue > maxValue) || (typedValue < minValue)) {
JOptionPane.showMessageDialog(IntegerTextField.this,
"The value can only be from " + getMinValue() + " to "
+ getMaxValue(), "Error Message",
JOptionPane.ERROR_MESSAGE);
} else {
super.insertString(offs, new String(str), a);
}
} catch (NumberFormatException ex) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(IntegerTextField.this,
"Only numeric values allowed.", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
}