0

我想在 CellList 组件中选择多个单元格;我是GWT的新手,请有人帮忙。为了获得多选,如何修改以下代码?

public class HelloWorld implements EntryPoint {
private static final List<String> DAYS = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday");

public void onModuleLoad() {
    // Create a cell to render each value.
    TextCell textCell = new TextCell();

    // Create a CellList that uses the cell.
    CellList<String> cellList = new CellList<String>(textCell);
    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    // Add a selection model to handle user selection.
    final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
    cellList.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
        public void onSelectionChange(SelectionChangeEvent event) {
            String selected = selectionModel.getSelectedObject();
            if (selected != null) {
                // Window.alert("You selected: " + selected);
            }
        }
    });

    cellList.setRowCount(DAYS.size(), true);

    // Push the data into the widget.
    cellList.setRowData(0, DAYS);

    // Add it to the root panel.
    RootPanel.get("gwtCellListBox").add(cellList);
}
}
4

2 回答 2

1

首先,您需要一个MultiSelectionModel. 然后,如果您想按住 Ctrl 键,请将 aDefaultSelectionEventManager用作始终返回andCellPreviewEvent.Handler的自定义。EventTranslatorTOGGLEfalse

于 2016-06-30T18:38:29.693 回答
0

尝试这个

public class HelloWorld implements EntryPoint {
private static final List<String> DAYS = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday");

public void onModuleLoad() {
    // Create a cell to render each value.
    TextCell textCell = new TextCell();

    // Create a CellList that uses the cell.
    CellList<String> cellList = new CellList<String>(textCell);
    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    // Add a selection model to handle user selection.
     final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<String>();
     cellList.setSelectionModel(selectionModel);
     selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
     public void onSelectionChange(SelectionChangeEvent event) {

        Set<String> selectedItems = selectionModel.getSelectedSet();

        for (String s : selectedItems) {
            System.out.println(s);
            Window.alert("You selected: " + s);
        }
    }
});

    cellList.setRowCount(DAYS.size(), true);

    // Push the data into the widget.
    cellList.setRowData(0, DAYS);

    // Add it to the root panel.
    RootPanel.get("gwtCellListBox").add(cellList);
}
}
于 2016-06-30T08:31:25.443 回答