wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Still keeping my fingers crossed, Here's the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Still keeping my fingers crossed, Here Watch "Still keeping my fingers crossed, Here New topic
Author

Still keeping my fingers crossed, Here's the code

Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi Guys
I'm still keeping my fingers crossed. Tried some stuff, but its not working yet.So, please fill me in with some inputs the moment you come up with something. This code is inside a funtion which initialises the constructor DateTimeField(myString, 22)
where mystring = "2000/11/11 10:59:10:10" => This is taking inputs more than 12 months in the months field and more than 31 in the days field. and likewise for the time set.
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.Date;
public class DateTimeField extends com.symantec.itools.javax.swing.JMaskedTextField {
private Toolkit toolkit;
private DateFormat dateFormatter;

public DateTimeField(String val, int columns) {
super(columns);
toolkit = Toolkit.getDefaultToolkit();
//dateFormatter = DateFormat.getDateInstance(new java.util.Locale("US", "US"));
//this.setDocument(new DateTimeDocument());
setValue(val);

}
public String getValue() {
String retVal = null;
//retVal = dateFormatter.parse(getText());

try {
retVal = dateFormatter.parse(getText()).toString();
} catch (ParseException e) {
// This should never happen because insertString allows
// only properly formatted data to get in the field.
toolkit.beep();
}

return retVal;
}
public void setValue(String val) {
//setText(parseValue(val));
setText(val);
//setText(dateFormatter.format(val).toString());
}
protected Document createDefaultModel() {
return new DateTimeDocument();
}
protected class DateTimeDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
char[] source = str.toCharArray();
char[] result = new char[source.length];
char[] result1 = new char[22];
int j = 0;int i =0;
// for all the chars in source check if its between our limits.
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[4] = source[4];
result1[j] = '/';j++;

//month
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 2) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 3))
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '1';
result1[j] = '1';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[7] = source[7];
result1[j] = '/';j++;
//date
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 4) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '1';
result1[j] = '1';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[10] = source[10];
result1[j] = '/';j++;
//hour
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i]) < 7) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[13] = source[13];
result1[j] = '/';j++;
//min
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 7) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[16] = source[16];
result1[j] = '/';j++;
//secj
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 7) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
//result[19] = source[19];
result1[j] = '/';j++;
//mili sec
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 7) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
toolkit.beep();
System.err.println("insertString: " + source[i]);
i++;j++;
}
if ( Character.isDigit(source[i]) && (Character.getNumericValue(source[i])< 10) )
{
result[i] = source[i];
result1[j] = source[i];
i++;j++;
}
else
{
result[i] = '0';
result1[j] = '0';
i++;j++;
toolkit.beep();
System.err.println("insertString: " + source[i]);
}
/*
String ss = new String(result, 0, 16);
System.out.println(" The new String is =" + ss);
super.insertString(offs, new String(result, 0, 16), a);
*/
String ss = new String(result1, 0, 22);
System.out.println(" The new String is ="+ss);
super.insertString(offs, new String(result1, 0, 22), a);
}
}
Thanks
Meghna
 
Don't get me started about those stupid light bulbs.
 
subject: Still keeping my fingers crossed, Here's the code