0

My program uses JDialogs to open up forms and in the form I want to use JCalendar for the user to select a date and for me to use it for other methods afterwards.

I have downloaded JCalendar library. I read some example codes but still not sure how to do it. I have an idea that in the form you press a button (Select Date) and like a small window opens with that JCalendar and when the date is selected it is displayed in the form as a TextField.

Can someone recommend me some method of doing this with the least trouble?

dic19
  • 17,821
  • 6
  • 40
  • 69
Limeran
  • 173
  • 1
  • 3
  • 13

1 Answers1

3

I have an idea that in the form you press a button (Select Date) and like a small window opens with that JCalendar and when the date is selected it is displayed in the form as a TextField.

You may want to try JDateChooser class present in JCalendar library, which allows selecting a date or type it manually. About the second part, you need to provide a PropertyChangeListener to the date chooser in order to listen the "date" property change and update the text field's text accordingly. For instance something like this:

final JTextField textField = new JTextField(15);

JDateChooser chooser = new JDateChooser();
chooser.setLocale(Locale.US);

chooser.addPropertyChangeListener("date", new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        JDateChooser chooser = (JDateChooser)evt.getSource();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        textField.setText(formatter.format(chooser.getDate()));
    }
});

JPanel content = new JPanel();
content.add(chooser);
content.add(textField);

JDialog dialog = new JDialog ();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
dic19
  • 17,821
  • 6
  • 40
  • 69
  • I didn't mean real time change of the textfield, just after the calendar is closed, the date should be shown to the user. My main question is how to implement that JCalendar or JDateChooser in my JDialog, how should I create it and display it. – Limeran Feb 21 '14 at 22:25
  • @Gedas sorry I misunderstood the textfield part (just disregard the `PropertyChangeListener` part). See my edit. I think it's quite simple, isn't it? – dic19 Feb 21 '14 at 22:31