开发者

Document Listener firing some times but not others

开发者 https://www.devze.com 2023-01-19 13:16 出处:网络
this one has been puzzling me for a few days now and I feel that I have barely been able to narrow it down.

this one has been puzzling me for a few days now and I feel that I have barely been able to narrow it down.

I am using Java and have a wizard for the user to step through. One of the steps allows the user to select a start time & date and an end time & date to schedule some work. I thought I had the validation on the dates complete (so that the end date must be after the start date & start date must be after current date etc). However, my validation method only fired once focus was lost on either date TextField so if the user selected a new date and immediately clicked next, an invalid choice could continue -- bug!

The start and end date selectors are widgets which are made up of a JSpinner and a calendar dialog which pops up if button is clicked. I have attached a Document Listener to the text field of the JSpinner:

DocumentListener docListener = new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            dateChanged();

            System.out.println("insertUpdate");

        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            dateChanged();
            System.out.println("removeUpdate");
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            dateChanged();
            System.out.println("changedUpdate");
        }

    };
    ((JSpinner.DefaultEditor) jSpinner1.getEditor()).getTextField().getDocument().addDocumentListener(
            docListener);

When I run this class using its own main method to test:

public static void main(String[] args) {

    DateSelectorWidget test = new DateSelectorWidget();
    JFr开发者_开发技巧ame f = new JFrame("T E S T ");
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(test, BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);

}

The DocListener fires each time and everything is fine. However this class is part of a bigger program and when it is called in it - the DocListener simply does not fire at all. An instance of the class is simply added to a panel in the wizard and yet it does not function the way it does when tested independently.

Any ideas anyone?

Thanks


My guess is that you directly or indirectly change the editor on the JSpinner after your line to retrieve, cast, get component, get model and add listener.

0

精彩评论

暂无评论...
验证码 换一张
取 消