Reputation: 31
I am trying to validate text field such that it accepts float or double values like .25, 0.2, 111.25
It should not accepts values like ...25, 0.2.2. etc
Upvotes: 1
Views: 6185
Reputation: 60
Create a class that extends PlainDocument and then set the jtextfield setDocument() to your new document class.
import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class DoubleDocument extends PlainDocument {
int size;
public DoubleDocument(int size) {
this.size = size;
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if(str == null){
return;
}
if(getLength() + str.length() > size){
Toolkit.getDefaultToolkit().beep();
return;
}
boolean isValid = true;
for(int i = 0; i < str.length(); i++){
if(!Character.isDigit(str.charAt(i))){
if(str.charAt(i) != '.'){
isValid = false;
break;
} else {
if(this.getText(0, this.getLength()).contains(".")){
isValid = false;
break;
}
}
}
}
if(isValid){
super.insertString(offs, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
then implement like this.
txtLoanAmount.setDocument(new DoubleDocument(12));
you can choose to remove the size in the DoubleDocument class if you don't care about the length of the field. Hope this helps.
Upvotes: 4
Reputation: 109823
otherwise (for plain JTextField
) you have to create an own DocumentFilter
with InputMask
and InputVerifier
JFormattedTextField
example
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public static void main(String args[]) {
JFrame frame = new JFrame("AbstractTextField Test");
final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.0);
formatter.setMaximum(1000.00);
return formatter;
}
});
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField2 = new JFormattedTextField(new Float(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
private DocumentListenerAdapter() {
}
}
Upvotes: 5
Reputation: 21793
-?(\d*\.)?\d+([eE][+\-]?\d+)?|[nN]a[nN]|[iI]nf(inity)?
is the regex I usually use for parsing doubles.
If you don't want infinities and NaNs,
-?(\d*\.)?\d+([eE][+\-]?\d+)?
If you also don't want exponential notation,
-?(\d*\.)?\d+
And don't forget to escape backslashes if you need to.
Upvotes: 3