java - 如何在java swt中的组合(下拉)中添加复选框?

标签 java eclipse checkbox combobox swt

我想在组合(下拉)中添加复选框,以便我可以一次选择组合中的多个元素。这可以做到吗?如果是,请解释一下或提供任何链接(如果可能)。

谢谢。

最佳答案

原始发帖人要求使用具有多选支持的下拉列表作为替代方案。 MultiSelectionCombo 的自定义实现可以达到此目的。


  1. 所选项目显示在文本字段中:

Drop down with multi-selection support http://s12.postimg.org/7ee1y6j5n/Multi_Selection_Combo.png

  • 按住Ctrl可选择多个项目:
  • Select multiple items in drop down http://s3.postimg.org/ufkezh99t/Items_selected.png

  • 获取所选项目的索引:
  • Get selected items http://s11.postimg.org/8n9fa7fb5/Get_selected_items.png


    MultiSelectionCombo的实现和演示:

    import java.util.Arrays;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.MouseAdapter;
    import org.eclipse.swt.events.MouseEvent;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.ShellAdapter;
    import org.eclipse.swt.events.ShellEvent;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.List;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    
    public class MultiSelectionCombo extends Composite {
    
       Shell    shell               = null;
       List     list                = null;
    
       Text     txtCurrentSelection = null;
    
       String[] textItems           = null;
       int[]    currentSelection    = null;
    
       public MultiSelectionCombo(Composite parent, String[] items, int[] selection, int style) {
          super(parent, style);
          currentSelection = selection;
          textItems = items;
          init();
       }
    
       private void init() {
          GridLayout layout = new GridLayout();
          layout.marginBottom = 0;
          layout.marginTop = 0;
          layout.marginLeft = 0;
          layout.marginRight = 0;
          layout.marginWidth = 0;
          layout.marginHeight = 0;
          setLayout(new GridLayout());
          txtCurrentSelection = new Text(this, SWT.BORDER | SWT.READ_ONLY);
          txtCurrentSelection.setLayoutData(new GridData(GridData.FILL_BOTH));
    
          displayText();
    
          txtCurrentSelection.addMouseListener(new MouseAdapter() {
    
             @Override
             public void mouseDown(MouseEvent event) {
                super.mouseDown(event);
                initFloatShell();
             }
    
          });
       }
    
       private void initFloatShell() {
          Point p = txtCurrentSelection.getParent().toDisplay(txtCurrentSelection.getLocation());
          Point size = txtCurrentSelection.getSize();
          Rectangle shellRect = new Rectangle(p.x, p.y + size.y, size.x, 0);
          shell = new Shell(MultiSelectionCombo.this.getShell(), SWT.NO_TRIM);
    
          GridLayout gl = new GridLayout();
          gl.marginBottom = 2;
          gl.marginTop = 2;
          gl.marginRight = 2;
          gl.marginLeft = 2;
          gl.marginWidth = 0;
          gl.marginHeight = 0;
          shell.setLayout(gl);
    
          list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
          for (String value: textItems) {
             list.add(value);
          }
    
          list.setSelection(currentSelection);
    
          GridData gd = new GridData(GridData.FILL_BOTH);
          list.setLayoutData(gd);
    
          shell.setSize(shellRect.width, 100);
          shell.setLocation(shellRect.x, shellRect.y);
    
          list.addMouseListener(new MouseAdapter() {
    
             @Override
             public void mouseUp(MouseEvent event) {
                super.mouseUp(event);
                currentSelection = list.getSelectionIndices();
                if ((event.stateMask & SWT.CTRL) == 0) {
                   shell.dispose();
                   displayText();
                }
             }
          });
    
          shell.addShellListener(new ShellAdapter() {
    
             public void shellDeactivated(ShellEvent arg0) {
                if (shell != null && !shell.isDisposed()) {
                   currentSelection = list.getSelectionIndices();
                   displayText();
                   shell.dispose();
                }
             }
          });
          shell.open();
       }
    
       private void displayText() {
          if (currentSelection != null && currentSelection.length > 0) {
             StringBuffer sb = new StringBuffer();
             for (int i = 0; i < currentSelection.length; i++) {
                if (i > 0)
                   sb.append(", ");
                sb.append(textItems[currentSelection[i]]);
             }
             txtCurrentSelection.setText(sb.toString());
          }
          else {
             txtCurrentSelection.setText("");
          }
       }
    
       public int[] getSelections() {
          return this.currentSelection;
       }
    
       // Main method to showcase MultiSelectionCombo
       // (can be removed from productive code)
       public static void main(String[] args) {
          Display display = new Display();
          Shell shell = new Shell(display);
          shell.setLayout(new GridLayout());
          shell.setText("MultiSelectionCombo Demo");
    
          // Items and pre-selected items in combo box
          String[] items = new String[] { "Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta", "Iota", "Kappa" };
          int[] selection = new int[] { 0, 2 };
    
          // Create MultiSelectCombo box
          final MultiSelectionCombo combo = new MultiSelectionCombo(shell, items, selection, SWT.NONE);
          combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
          ((GridData)combo.getLayoutData()).widthHint = 300;
    
          // Add button to print current selection on console
          Button button = new Button(shell, SWT.NONE);
          button.setText("What is selected?");
          button.addSelectionListener(new SelectionAdapter() {
             public void widgetSelected(SelectionEvent event) {
                System.out.println("Selected items: " + Arrays.toString(combo.getSelections()));
             }
          });
    
          shell.pack();
          shell.open();
          while (!shell.isDisposed()) {
             if (!display.readAndDispatch()) {
                display.sleep();
             }
          }
          display.dispose();
       }
    
    }
    

    关于java - 如何在java swt中的组合(下拉)中添加复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673121/

    相关文章:

    java - 亚马逊 SQS : The same message is consumed by two current consumers

    java - 为什么要在 OSGi 中创建和使用服务

    java - 如何从属性文件定义注释的字段值

    java - 为什么Eclipse中的显示不支持Java中的递归函数?

    java - 需要帮助获取返回窗口显示图像的函数

    android - YYY 项目的构建路径上没有任何 GWT SDK

    eclipse - 在 Dart 插件中启用 Chromium/Dartium

    android - TextView 右侧的 CheckBox

    javascript - 如何使用模运算删除 ng-repeat 生成的未定义复选框?

    mysql - Laravel 如何将多个复选框值保存到 SQL 数据库的不同行中?