在组合框eclipse插件中获取所选项目的价值

Getting value of selected item in combo box eclipse plugin

如何从组合框中获取所选项目的值?我在执行saveInput方法的时候一直报空指针异常

这是我的对话代码:

public class MyTitleAreaDialog extends TitleAreaDialog {

    private String age;    
    private Combo combo;    

    public MyTitleAreaDialog(Shell parentShell) {
        super(parentShell);
    }

    @Override
    public void create() {
        super.create();
        setTitle("Age Box");
        setMessage("Please enter your info..", IMessageProvider.INFORMATION);
    }

    @Override
    protected Control createDialogArea(Composite parent) {            
        Combo combo = new Combo(container, SWT.READ_ONLY);
        combo.setItems(new String[] {"21", "22", "23"});    
        return area;
    }

    private void saveInput() {        
        if(combo.getSelectionIndex() >= 0){
            age = combo.getItem(combo.getSelectionIndex());
        }        
    }

    @Override
    protected void okPressed() {
        saveInput();
        super.okPressed();
    }

    public String getSelectedAge() {
        return age;
    }        
}

这是处理程序代码:

public class SampleHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

        MyTitleAreaDialog dialog = new MyTitleAreaDialog(window.getShell());

        dialog.create();
        if (dialog.open() == Window.OK) {           
            System.out.println(dialog.getSelectedAge());            
        }       
        return null;
    }
}

如果有任何帮助,我将不胜感激。我是 Java 和 eclipse 编程的新手...谢谢!!

您的 createDialogArea 方法正在将 Combo 分配给名为 combolocal 变量,not 名为 combo.

的字段

替换:

Combo combo = new Combo(container, SWT.READ_ONLY);

combo = new Combo(container, SWT.READ_ONLY);