tags:

views:

31

answers:

3

I want to create component in Swing where text field takes only date type value.

+1  A: 

Take a look at SwingX library. It is free and has a very good component called JXDatePicker

eugener
Thanks for pointing out this library... looks great...
Pete
A: 

There are a few options for you:

  • create an editable text field that parses for a date format on insert, using a DocumentListener or a custom Document
  • create an editable text field that gets parsed for a valid date format on loss of focus or validated on an ActionEvent triggered by a submit button
  • create a non-editable text field with spinner buttons that increment/decrement the date string that you define
  • use a 3rd party library as @eugener suggests

If you have a specific problem creating your component, please post the code you have been working on and we will be happy to help further.

akf
+2  A: 

You could use JFormattedTextField, setting your format for the date. Works without using anything outside of the JRE, but might not be the most usable option.

Otherwise do what eugener said and use SwingX's JXDatePicker or JIDE's DateComboBox.

Trejkaz