Search code examples
javaswtjfacercp

How to create a dialog with checkboxes that allows multiple selections


Having read this link, I want to create a dialog where I can select multiple items which to be in the form of check boxes. ElementListSelectionDialog doesn't show any elements in the dialog.

final ElementListSelectionDialog dialog = new     ElementListSelectionDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), new LabelProvider());
dialog.setElements(new String[] { "1", "2", "3" });
dialog.setTitle("Numbers");
dialog.setMultipleSelection(true);
dialog.setInitialSelections(new String[] { "1", "2", "3" });
dialog.open();

Using the code above, I get the items in the dialog sometimes, but there are two problems:

  1. I only get them sometimes
  2. It is not in the form of checkboxes.

If you know a better way of doing this, please let me know. I really appreciate any help :)


Solution

  • ElementListSelectionDialog does not support check box selection.

    You may be able to use org.eclipse.ui.dialogs.ListSelectionDialog:

    String [] elements = ...
    
    ListSelectionDialog dialog = 
       new ListSelectionDialog(shell, elements, ArrayContentProvider.getInstance(),
                new LabelProvider(), "selection message");
    
    dialog.setTitle("dialog title");
    
    dialog.setInitialSelections(new Object []{....selections});
    
    dialog.open();
    
    Object [] result = dialog.getResult();
    

    or you can always write your own dialog using CheckboxTableViewer.