MishkuMoss
MishkuMoss

Reputation: 118

Need multiselection in GWT CellList

I would like to select multiple cells in a CellList component; I'm new to GWT, someone please help. In order to get multi selection, how do I modify the below code?

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);
}
}

Upvotes: 0

Views: 295

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64561

First, you need a MultiSelectionModel. Then, if you want to have hold the Ctrl key, use a DefaultSelectionEventManager as the CellPreviewEvent.Handler with a custom EventTranslator that always return TOGGLE and false.

Upvotes: 1

Clement Amarnath
Clement Amarnath

Reputation: 5476

Try this

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);
}
}

Upvotes: 0

Related Questions