Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField and DateFormat problems  RSS feed

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I need JForemattedTextField wich will format date ("MM/dd/yy"). I have the next snippet of code



The problem is: if the date is "07/26/04" for example and if i try to input 12 on month I cant do it because when I type 1 it understands the month as 17 and doesnt give me to input the next digit.
I need that when I type for example 12 on month position then the JFormattedTextField will check the correctness of the month only after I typed the second digit, and if it is incorrect the month will return back to previous value.
How can I solve this?

Thanks in advance
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I can get this to work in j2se 1.4.2 is to comment–out this line
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig.

Well if I comment that string out, I will allow to user incorrect input.

If there some classes or interfaces wich I can extend or implement to define the behavior of JFormattedTextField when the incorrect input was detected.
For example if user typed 15 on month position I'd like to return back the previous month immediatly (not after focus was lost).

And second question: if I will comment that string the "/" simbols may be deleted if user will type for example three digitson month position instead of two. How can I prevent this situation?

Thanks in advance
 
Nadinha Jovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can subclass Document and in it provide implementation for the insertString model. In this model you can control users input. Here is an example of Document allowing integers only to be entered in a text field.

import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class IntegerDocument extends PlainDocument {

public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {

char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i < result.length; i++) {
//it is ok for the first char to be '-'
if (Character.isDigit(source[i]))
result[j++] = source[i];
else if (offs==0 && i==0 && source[i]=='-')
result[j++] = source[i];
else
Toolkit.getDefaultToolkit().beep();
}
super.insertString(offs, new String(result, 0, j), a);
}

}


And here is the IntegerTextField class and set its default document model to be the document model class you created.


import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import javax.swing.JTextField;
import javax.swing.text.Document;
import mq.psy.jwatch.ui.swing.document.IntegerDocument;

public class IntegerTextField extends JTextField {

private NumberFormat integerFormatter;

public IntegerTextField(int columns) {
super(columns);
integerFormatter = NumberFormat.getIntegerInstance(Locale.US);
integerFormatter.setParseIntegerOnly(true);
}

public IntegerTextField() {
this(0);
}

protected Document createDefaultModel() {
return new IntegerDocument();
}

}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is basically a combination of your DateFormat, a DocumentFilter and an InputVerifier. I added in some cut, copy and paste so you can give this a test drive. It doesn't do everything you wanted but you can try it out and decide if it's worth the effort to continue.
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!