简体   繁体   English

如何使用相同的事件处理程序在GWT(或Java AWT Swing)中的相同种类的多个对象上执行相同的功能

[英]How can I use the same Event Handlers Performing same functions on multiple Objects of the same Kind in GWT (or Java AWT Swing)

I just started working on Gwt2.0. 我刚刚开始研究Gwt2.0。 I have two textbox here. 我在这里有两个文本框。 Both perform the same event operation. 两者都执行相同的事件操作。 Using 使用

addFocusListener()

What I have now is. 我现在所拥有的是。

areaBox.addFocusListener(new FocusListener(){

        @Override
        @Deprecated
        public void onFocus(Widget arg0) {
            // TODO Auto-generated method stub
            areaBox.setTitle("Area");
        }
            @Override
            public void onLostFocus(Widget arg0) {
                if(areaBox.getText().length() >= 4 )
                {
                    areaBox.setStyleName("gwt-TextBox-Success");
                }
                else
                {
                    areaBox.setStyleName("gwt-TextBox-Error");
                    cityBox.setText("Enter Area Name ");
                    areaBox.addFocusListener(new FocusListener(){

                        @Override
                        @Deprecated
                        public void onFocus(Widget arg0) {
                            areaBox.setText(null);
                            areaBox.setStyleName("gwt-TextBox-AE");

                        }

                        @Override
                        @Deprecated
                        public void onLostFocus(Widget arg0) {

                        }

                    });

                }
            }

});
cityBox.addFocusListener(new FocusListener(){

    @Override
    @Deprecated
    public void onFocus(Widget arg0) {
        cityBox.setTitle("City");
    }
        @Override
        public void onLostFocus(Widget arg0) {
            if(cityBox.getText().length() >= 4 )
            {
                cityBox.setStyleName("gwt-TextBox-Success");
            }
            else
            {
                cityBox.setStyleName("gwt-TextBox-Error");
                cityBox.setText("Enter City Name ");

                cityBox.addFocusListener(new FocusListener(){

                    @Override
                    @Deprecated
                    public void onFocus(Widget arg0) {
                        cityBox.setText(null);
                        cityBox.setStyleName("gwt-TextBox-AE");

                    }

                    @Override
                    @Deprecated
                    public void onLostFocus(Widget arg0) {
                        // TODO Auto-generated method stub

                    }

                });

            }
        }

}); 

What the code does is. 该代码的作用是。 When Focused on Area TextBox, a tooltip appears, showing what to enter. 当聚焦于区域文本框时,将显示一个工具提示,显示要输入的内容。 When Focus on it is lost or comes to next textbox ie City TextBox, it checks whether the entered string is greater than 4 characters. 当焦点丢失或进入下一个文本框(例如,城市文本框)时,它将检查输入的字符串是否大于4个字符。 if yes then SUCCESS css style is applied to the box, if not ERROR css style is applied. 如果是,则将SUCCESS CSS样式应用于该框,如果不是ERROR,则应用CSS样式。 So, when clicked on it(AreaBox) again, The text is cleared and css style is reset. 因此,再次单击它(AreaBox)时,将清除文本并重置css样式。

The above is a sample between 2 textbox. 上面是2个文本框之间的示例。 Please help me, I have nearly 10 such fields, I want to minimize the code. 请帮助我,我有将近10个这样的字段,我想最小化代码。 I am thinking of using collections or custom widgets. 我正在考虑使用集合或自定义窗口小部件。 But don't know where to start. 但是不知道从哪里开始。 Need your help and opinion. 需要您的帮助和意见。 Thanks... 谢谢...

Here is how I would solve this problem: 这是我如何解决此问题的方法:

areaBox.addFocusListener(generateFocusListener("Area", "Enter Area Name "));
cityBox.addFocusListener(generateFocusListener("City", "Enter City Name "));

FocusListener generateFocusListener(final String title, final String text) {
    return new FocusListener(){

            @Override
            @Deprecated
            public void onFocus(Widget widget) {
                // initial focus happens here
                TextBox box = (TextBox) widget;
                box.setTitle(title);
            }

            @Override
            public void onLostFocus(Widget widget) {
                // Focus is lost for the first time

                TextBox box = (TextBox) widget;
                if(box.getText().length() >= 4 )
                {
                    box.setStyleName("gwt-TextBox-Success");

                    // Change focus listener so that now once pressed it resets
                    box.addFocusListener(new FocusListener(){

                        @Override
                        @Deprecated
                        public void onFocus(Widget widget) {
                            TextBox box = (TextBox) widget;
                            box.setText(null);
                            box.setStyleName("gwt-TextBox-AE");
                        }

                        @Override
                        @Deprecated
                        public void onLostFocus(Widget widget) {
                            TextBox box = (TextBox) widget;
                            if(box.getText().length() >= 4 )
                            {
                                box.setStyleName("gwt-TextBox-Success");
                            }
                            else
                            {
                                box.setStyleName("gwt-TextBox-Error");
                                box.setText(text);
                            }

                        }

                    });
                }
                else
                {
                    box.setStyleName("gwt-TextBox-Error");
                    box.setText(text);
                }
            }

    };
}

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

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