Using JCalendar in a JDialog

前端 未结 1 798
不知归路
不知归路 2021-01-29 04:17

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

相关标签:
1条回答
  • 2021-01-29 04:30

    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);
    
    0 讨论(0)
提交回复
热议问题