简体   繁体   English

文档监听器有时会触发,但其他时候不会

[英]Document Listener firing some times but not others

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. 我正在使用Java,并为用户提供了一个向导。 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! 但是,我的验证方法仅在焦点丢失在任一日期TextField上时才触发,因此,如果用户选择一个新日期并立即单击下一步,则无效选择可能会继续-错误!

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. 开始日期和结束日期选择器是由JSpinner和单击按钮会弹出的日历对话框组成的小部件。 I have attached a Document Listener to the text field of the JSpinner: 我已将文档侦听器附加到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: 当我使用其自己的main方法运行此类进行测试时:

public static void main(String[] args) {

    DateSelectorWidget test = new DateSelectorWidget();
    JFrame 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. DocListener每次都会触发,一切都很好。 However this class is part of a bigger program and when it is called in it - the DocListener simply does not fire at all. 但是,此类是更大程序的一部分,当在其中调用时-DocListener根本不会触发。 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 谢谢

我的猜测是,您可以在行之后直接或间接更改JSpinner上的编辑器,以检索,强制转换,获取组件,获取模型并添加侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM