简体   繁体   English

从 GWT 中的不同小部件触发处理程序事件

[英]Firing handler event from different widget in GWT

I got two ListBoxes, assume customerListBox and timeListBox , and Label resultLabel .我有两个 ListBox,假设customerListBoxtimeListBox以及 Label resultLabel When timeListBox 's value is changed, it fires its ValueChangeHandler (let's call it recalculateValueHandler ) which recalculates result and put it into resultLabel .timeListBox的值改变时,它会触发它的ValueChangeHandler (我们称之为recalculateValueHandler ),它重新计算结果并将其放入resultLabel I need this to work vice versa - when customerListBox 's value is changed I need to recalculate new result for same time value but different customer.我需要反之亦然 - 当customerListBox的值发生变化时,我需要重新计算相同时间值但不同客户的新结果。 So I'd need something like customerListBox.onValueChange(fire(recalculateValueHandler)) , hope you do understand.所以我需要一些类似customerListBox.onValueChange(fire(recalculateValueHandler))的东西,希望你能理解。 Is there anything that could work for me this way?有什么可以以这种方式对我有用吗? I try to avoid duplicating pretty much same code into both handlers.我尽量避免将几乎相同的代码复制到两个处理程序中。

You just need three things你只需要三样东西

  • declare all elements at top level so they are accessible across all methods在顶层声明所有元素,以便它们可以在所有方法中访问
  • create a method that recalculates the value, let's call it recalculateValue创建一个重新计算值的方法,我们称之为recalculateValue
  • add ChangeHandler s for both ListBox es ( ListBox don't have ValueChangeHandler ) that will call recalculateValue method.为将调用recalculateValue方法的两个ListBox es( ListBox没有ValueChangeHandler )添加ChangeHandler

Working example:工作示例:

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;

public class Test implements EntryPoint {

    // elements declared at top level are accessible across all methods
    private ListBox customerListBox;
    private ListBox timeListBox;
    private Label resultLabel;

    @Override
    public void onModuleLoad() {
        // add some data
        customerListBox = new ListBox();
        for(int i = 0; i < 10; i++)
            customerListBox.addItem("Customer " + (i + 1));

        // add some data
        timeListBox = new ListBox();
        for(int i = 0; i < 10; i++)
            timeListBox.addItem("Time " + (i + 1));

        resultLabel = new Label();
    
        // recalculateValue when customerListBox changes
        customerListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                recalculateValue();
            }
        });
    
        // recalculateValue when timeListBox changes
        timeListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                recalculateValue();
            }
        });

        // initial result (optional)
        recalculateValue();

        // show elements
        RootPanel.get().clear();
        RootPanel.get().add(customerListBox);
        RootPanel.get().add(timeListBox);
        RootPanel.get().add(resultLabel);
    }

    private void recalculateValue() {
        // use values from ListBoxes
        resultLabel.setText(customerListBox.getSelectedValue() + " / " + timeListBox.getSelectedValue());
    }
}

Notice that both handlers ares identical, so you can create only one handler and use it for both ListBox es like that:请注意,两个处理程序是相同的,因此您只能创建一个处理程序并将其用于两个ListBox es,如下所示:

ChangeHandler handler = new ChangeHandler() {
    @Override
    public void onChange(ChangeEvent event) {
        recalculateValue();
    }
};

// recalculateValue when customerListBox changes
customerListBox.addChangeHandler(handler);

// recalculateValue when timeListBox changes
timeListBox.addChangeHandler(handler);

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

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