• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Special TextEdit control for Date

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The application I am working on requires the end-user to enter dates. I would like to include a special control, something similar to the DateTimePicker control in VC++ wherein the user can choose a Date/Time. As of now, I can only put a JTextEdit control which will input a string which should be compatible with a Date format. I am not satisfied with this control.
Please suggest if I can get a readymade JavaBean for this. Moreover, I wish to avoid using any vendor specific control because the application is supposed to be platform independent.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friend,

I've written a piece of code on my own to enter date in the JTextField in the format ("mm/dd/yyyy") I'm sending my code below. Hope this will satisfy your requirements. You can change my code if you Like.
JTextField jtextField = new JTextField();
Use "jtextfield.setDocument(new DateDocument(...));"

import javax.swing.text.*;
import java.awt.Font;
class DateDocument extends PlainDocument {
public static String initString = "mm/dd/yyyy";
private int sep1 = 2, sep2 = 5;
private JTextComponent textComponent;
private int newOffset, yearVal = 0;
public DateDocument(JTextComponent tc) {
textComponent = tc;
textComponent.setFont(new Font("TimesRoman", Font.BOLD, 14));
try {
insertString(0, initString, null);
initString = "mm/dd/yyyy";
}
catch(Exception ex) { ex.printStackTrace(); }
}
private boolean resetDateByMonth(int dd, int mm, AttributeSet attributeSet) throws Exception
{
int maxVal = getDaysInMonth(mm, yearVal);
if(dd > maxVal)
{
super.remove(3, 2);
super.insertString(3, Integer.toString(maxVal), attributeSet);
return (true);
}
return(false);
}
private void resetDateByYear(int mm, int dd, int yy, AttributeSet attributeSet) throws Exception
{
if((mm == 2) && ((yy % 4) != 0) && (dd == 29))
{
super.remove(4, 1);
super.insertString(4, "8", attributeSet);
}
}
public void insertString(int offset, String s,
AttributeSet attributeSet)
throws BadLocationException {
if(s.equals(initString)) {
super.insertString(offset, s, attributeSet);
}
else {
try {
Integer.parseInt(s);
}
catch(Exception ex) {
return;
}
newOffset = offset;
int value = Integer.parseInt(s);
try
{
yearVal = Integer.parseInt(getText(6, 4));
}catch(Exception e) {}
if(newOffset == 0)
{
try
{
if(value > 1)
return;
int m2 = Integer.parseInt(getText(1, 1));
if((value == 1) && (m2 > 2))
{
super.remove(1, 1);
super.insertString(1, "0", attributeSet);
m2 = 0;
}
int dd = Integer.parseInt(getText(3, 2));
resetDateByMonth(dd, (value * 10 + m2), attributeSet);
}
catch(Exception e) {}
}
else if(newOffset == 1)
{
try
{
int m1 = Integer.parseInt(getText(0, 1));
if((value > 2) && (m1 == 1))
s = "2";
int dd = Integer.parseInt(getText(3, 2));
resetDateByMonth(dd, (m1 * 10 + value), attributeSet);
}
catch(Exception e) {}
}
else if((newOffset == 2) | | (newOffset == 3))
{
try
{
if(value > 3)
return;
int mm = Integer.parseInt(getText(0, 2));
int d2 = Integer.parseInt(getText(4, 1));
if(resetDateByMonth((value * 10 + d2), mm, attributeSet))
{
textComponent.setCaretPosition(4);
return;
}
}catch(Exception e) {}
}
else if(newOffset == 4)
{
try
{
int d1 = Integer.parseInt(getText(3, 1));
if((d1 == 3) && (value > 1))
return;
int mm = Integer.parseInt(getText(0, 2));
if(resetDateByMonth((d1 * 10 + value), mm, attributeSet))
{
textComponent.setCaretPosition(6);
return;
}
}catch(Exception e) {}
}
else if((newOffset == 5) | | (newOffset == 6))
{
try
{
if((value > 2) | | (value < 1))<br /> return;<br /> int y1 = Integer.parseInt(getText(7, 1));<br /> if((value == 2) && (y1 > 0))
{
super.remove(7, 1);
super.insertString(7, "0", attributeSet);
textComponent.setCaretPosition(8);
}
else if((value == 1) && (y1 < 9))
{
super.remove(7, 1);
super.insertString(7, "9", attributeSet);
textComponent.setCaretPosition(8);
}
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int yh = Integer.parseInt(getText(7, 3));
resetDateByYear(mm, dd, (value * 1000 + yh), attributeSet);
}catch(Exception e) {}
}
else if(newOffset == 7)
{
try
{
int y1 = Integer.parseInt(getText(6, 1));
if((y1 == 1) && (value < 9))
s = "9";
else if((y1 == 2) && (value > 0))
s = "0";
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int yh = Integer.parseInt(getText(8, 2));
resetDateByYear(mm, dd, (y1 * 1000 + value * 100 + yh), attributeSet);
}catch(Exception e) {}
}
else if((newOffset == 8) | | (newOffset == 9))
{
try
{
int yh = Integer.parseInt(getText(6, 2));
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int y3 = 0, y4 = 0;
if(newOffset == 8)
{
y3 = value;
y4 = Integer.parseInt(getText(9, 1));
}
else
{
y3 = Integer.parseInt(getText(8, 1));
y4 = value;
}
resetDateByYear(mm, dd, (yh * 100 + y3 * 10 + y4), attributeSet);
}catch(Exception e) {}
}
if(atSeparator(offset)) {
newOffset++;
textComponent.setCaretPosition(newOffset);
}
super.remove(newOffset, 1);
super.insertString(newOffset, s, attributeSet);
}
}
public void remove(int offset, int length)
throws BadLocationException {
if(atSeparator(offset))
textComponent.setCaretPosition(offset-1);
else
textComponent.setCaretPosition(offset);
}
private boolean atSeparator(int offset) {
return offset == sep1 | | offset == sep2;
}
private int getDaysInMonth(int monthValue, int yy)
{
switch(monthValue)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return (31);
case 2:
{
if((yy % 4) == 0)
return (29);
else
return (28);
}
case 4:
case 6:
case 9:
case 11:
return (30);
}
return(31);
}
}

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen
I was trying to compile and run the code you'd mailed long back on Javaranch. This is regarding how the user formats Date in a JTextComponent. I have some requirements for my application.
Right now, my application has a bunch of Labels and Textfields.
and they're called Name and Value (Vector of String) both. Now, the Value attribute depends on the data inside another attibute called Syntax which is of datatype String. I've described the graph in detail below
Here it is :

1. Label => Name, Textfield => Value
2. Now the Value attribute depends on attributes such as :
a. Syntax => String data type => eg : "Integer", "Date/Time", "String" etc.
b. Max Value => String data type => eg. "100"
c. Min Value => String data type => eg. "10"
Now, my question :
Depending on the string passed in syntax, the Value (textfield)should format itself to accept the respective datatypes.
i.e If
a. Syntax => "integer", Value => Integer i/p only
b. Syntax => "Date/Time", Value => format itself to accept Date/ Time only. For user i/p a calender should popup
c. Syntax => "IPAddress", Value => format itself to accept IPAddress only
I'm done with part (a). How do I go about doing part(b) and part(c) ?!!!
I would greatly appreciate your help. Please fill me in with a solution .
Thanks
Meghna

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I found your code out here and thought I would give it a shot. However, when I try to compile it, I get the following compiler messages:
I'm not really sure what its trying to tell me,
Could someone help me out?

Thanks for any help!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sure looks like a typical compiler error message when missing a ")", "(", "}", and/or "{" ...
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i tried compiling the code and was successful with a few corrections like removing the space in between the OR statements.
its like this in the code,
return offset == sep1 | | offset == sep2;
modified to
return offset == sep1 || offset == sep2;
and do it the same way for the rest of the 2 errors also. it will compile
Raj
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip. It was the spaces causing my problem.
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic