Chitrang
Chitrang

Reputation: 11

Simple Date Component in Swing

I was looking for open source for Date text field component. I tried some but I was looking for a component where user has to enter just numbers. User doesn't have to enter the separator of day, month and year. For example, for the date '08-23-2015' (date format is MM-dd-yyyy) user just need to enter 08232015. He doesn't have to care about the separator (-).

Also when user moves caret back and forth in text field, it should skip the separator.
For example,

Initial caret position: |08-23-2015
User moves caret right: 0|8-23-2015
User moves caret right: 08-|23-2015
User moves caret left : 0|8-23-2015

Also it should support minimum date and maximum date constraint.

I have created a simple date component which I am sharing here, if someone is looking for similar kind of componet.

It can be fit for following formats

Source Link: https://github.com/ChitrangP/SimpleDateComponent

Upvotes: 0

Views: 224

Answers (1)

user4257136
user4257136

Reputation:

Buddy it is a big work.

  • First step

Create a Number_Filter class (for check whether the entered value is number or not.) (If you need that class,please ask to me )


  • Second step

Create your project and add necessary components,including the Text box for date entering.

Don't forget to add class to project file and create an object,like

Number_Filter obj_name = new Number_Filter();

  • Third step

on change event of text box-- add this much of code

a)Check number format by using

obj_name(Text_Date); //if not number it should clear the last typed digit/letter

b)Check the length of user input and put a slash or hyphen (/or-)after first 2 digits-means(DD)of your date

if(Text_Date.gettext.toString.length()==1){  // second position
   Text_Date.settext(Text_Date.gettext.toString+"-")
  }   

for eg-: if user type 28 ,at the same time (/or-) should be added.(write code with respect to that basis)

c)Same process should continue on next to digits-means (MM)

if(Text_Date.gettext.toString.length()==4){  // fifth position
   Text_Date.settext(Text_Date.gettext.toString+"-")
  }

for eg-: if user type 02 ,at the same time (/or-) should be added.(write code with respect to that basis)(DD-MM-)-->(28-02-)

c)Check the value in that date field which does not exceed 10 digits.like

if(Text_date.gettext.toString.length()>=9){
   //write code for delete the last inputed digit 
 }

Read it carefully and Do it till this

Upvotes: 2

Related Questions