SWT中comboViewer 控件使用

来源:互联网 发布:html显示xml数据 编辑:程序博客网 时间:2024/04/19 06:39
package com.test.swt;

import org.eclipse.swt.widgets.Display;

public class TestComboViewer {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestTree window = new TestTree();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(645, 440);
        shell.setText("下拉列表框的使用");
        final ComboViewer comboViewer = new ComboViewer(shell, SWT.NONE);
        final Combo combo = comboViewer.getCombo();
        combo.add("张三", 0);
        combo.add("张二", 1);
        combo.add("张一", 2);
        combo.add("张晓", 3);
        combo.add("张飞", 4);
        combo.add("张宇", 5);
        //设置默认值
        combo.select(0);
        comboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        
            public void selectionChanged(SelectionChangedEvent org) {
                //String index=
//                int index=comboViewer.getCombo().getSelectionIndex();
//                String num=comboViewer.getCombo().getText();
//                 MessageDialog.openInformation(shell, "信息", "选择了:"+index+":"+num);
                
                int index=combo.getSelectionIndex();
                String num=combo.getText();
                 MessageDialog.openInformation(shell, "信息", "选择了:"+index+":"+num);
            }
        });

        
        
        
        combo.setBounds(156, 90, 130, 25);

    }
}