如何将CheckBox强制转换为HasChangeHandler

时间:2011-11-21 10:42:42

标签: java gwt checkbox

CheckBox faxGen = new CheckBox();
((HasChangeHandlers) faxGen).addChangeHandler(new ChangeHandler(){
    public void onChange(ChangeEvent event) 
    {
        CheckBox chkBox = (CheckBox) event.getSource(); 
        FixedWidthGrid parent = (FixedWidthGrid) chkBox.getParent();
        Iterator<Integer> selectedRows = parent.getSelectedRows().iterator();
        Integer rowIdx = selectedRows.hasNext() ? selectedRows.next() : -1;
        boolean checked = chkBox.getValue() == true ? true : false;
        setCheckBoxRowSelected(rowIdx, checked, true);
        if (checked){
            pagingScrollTable.getDataTable().getRowFormatter().setStylePrimaryName(rowIdx, "grid2-body-row-hover");
        }
        else
        {
            pagingScrollTable.getDataTable().getRowFormatter().removeStyleName(rowIdx, "grid2-body-row-hover");
            pagingScrollTable.getDataTable().getRowFormatter().removeStyleName(rowIdx, "selected");
        }
        pagingScrollTable.getDataTable().getRowFormatter().removeStyleName(rowIdx, "highlighted selected");
    }
});

1 个答案:

答案 0 :(得分:2)

您无法将CheckBox转换为HasChangeHandlers实例,因为它未实现该接口。 CheckBox实现HasValueChangeHandlers,以便您可以根据需要进行投射。但是,从这段代码中,演员的价值还不清楚。由于您知道faxGen是一个CheckBox(您在添加处理程序的行上方声明并实例化它),因此您应该添加处理程序(valueChangeHandler或ClickHandler)而不进行强制转换。< / p>