0

I'm creating a swing app with two date fields(date picker) and a button, and the case is like this.

  1. Initially, the button will be disabled
  2. if the startDateChooser date is not null, then this button should be enabled.
  3. if again the startDateChooser date is null(i.e. clearing off the data), this button should be disabled.

I'm able to do the button to be disabled by default, can someone please let me know on how can I do this. I'm basically confused on which event to bind this to.

Below is my current code.

private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 732, 502);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        final JDateChooser startDateChooser = new JDateChooser();

        startDateChooser.setBounds(49, 150, 209, 52);
        frame.getContentPane().add(startDateChooser);

        final JDateChooser endDateChooser = new JDateChooser();
        endDateChooser.setBounds(339, 150, 209, 53);
        frame.getContentPane().add(endDateChooser);

        final JButton btnGenerateEmails = new JButton("Generate Emails");
        btnGenerateEmails.setEnabled(false);
        startDateChooser.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                btnGenerateEmails.setEnabled(true);
            }
        });
        btnGenerateEmails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
             JOptionPane.showMessageDialog(null, "Hello There!");
            }
        });
        btnGenerateEmails.setBounds(271, 360, 209, 70);
        frame.getContentPane().add(btnGenerateEmails);

        JList list = new JList();
        list.setBounds(271, 116, 87, -31);
        frame.getContentPane().add(list);

    }

going through the solution available at Is it possible to detect a date change on a JCalendar JDateChooser field?

I've replaced

startDateChooser.addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    btnGenerateEmails.setEnabled(true);
                }
            });

with

startDateChooser.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent e) {
                btnGenerateEmails.setEnabled(true);
            }
        });

and after doing this change, my button is being enabled by default.

Thanks.

Community
  • 1
  • 1
user3872094
  • 3,269
  • 8
  • 33
  • 71
  • Which implementation of `JDateChooser` are you using? You "could" use a `FocusListener` on the editor field, if you can get access to it, but many will generate a `PropertyChangeEvent` when the date changes, it what you should use will come down to which `JDateChooser` you're using (from which library) – MadProgrammer Mar 15 '17 at 20:53
  • @MadProgrammer, thanks for the quick respose i'm using `jCalendar1.4` – user3872094 Mar 15 '17 at 20:54
  • You might need to do some validation on the `PropertyChangeEvent` (e.g. what property is changing and what the new value of the property is) as it is likely that there are events fired during the construction/display of the component which will then set your button enabled – Java Devil Mar 15 '17 at 21:08
  • @JavaDevil, this is what I'm unable to understand, can you please let me know what and how can I do this? – user3872094 Mar 15 '17 at 21:12
  • @user3872094 When the `PropertyChange` events are triggered, you need to determine the current state, based on your rules and enabled/disable the button accordingly – MadProgrammer Mar 15 '17 at 21:15
  • Look at the docs for [`PropertyChangeEvent`](https://docs.oracle.com/javase/7/docs/api/java/beans/PropertyChangeEvent.html). A simple way to see this would be just to print out the event every time it is fired. From that you should be able to see what property you are after and write some logic to handle the button enabling appropriately. – Java Devil Mar 15 '17 at 21:20
  • Get the date as a string and then check if string is null than disable your date chooser – Muhammad Yawar Mar 15 '17 at 21:42

0 Answers0