views:

475

answers:

2

I'm currently working on a Swing app and I've got a few JTextAreas that are going to be parsed, turned into dates and then added to a MySQL database. One is a Date field, the others are DateTime, what I'm trying to do is use InputVerifier to make sure they're entered correctly.

I've created an InputVerifier that tries to turn the text into a date useing DateFormat.parse() and that mostly works, however there are two flaws: Firstly it can't check if I only have a Date, rather than a date and time Secondly it can't check if I have both a date and time, rather than just one of them.

Is there any way around this? Or a better way to validate date fields in Swing I'm not aware off?

+1  A: 

Just a thought: you could consider using a SpinnerDateModel with a JSpinner, instead of a text field. Maybe you could handle the date-only field by calling setCalendarField(Calendar.DAY) - never done it myself, though, so I'm not sure.

David Zaslavsky
+1 for only selecting the Day field. Not perfect but it helps
Benjamin Confino
+2  A: 

Swing has support for entering dates into text fields using a JFormattedTextField with a DateFormatter. With a DateFormatter you can specifiy a DateFormat object so can have either a date only field or a date and time field.

Have a look at How To Use Formatted Text Fields in the Swing tutorial as an introduction.

Mark