JFace ComboViewer难度

时间:2017-08-01 14:22:26

标签: java jface viewer

我有一些像这样的键值对:

1 - 鲍勃

2 - Christine

3 - 迈克尔

我把这些数据放在一个组合查看器中,但是当我选择例如2-Christine时,我希望我的组合查看器只向我显示密钥。

我为comboviewer添加了一个SelectionChangedListener,但我不知道如何更改组合框在您选择的内容后显示的内容。

1 个答案:

答案 0 :(得分:0)

您可以使用Combo的SWT可编辑模式来实现它。当您进入SWT的只读模式时,输出不可修改。我们有类似的要求唯一的区别是在显示框的末尾添加一个文本&我们使用的是简单的SWT Combo。

如果有帮助,请告诉我。 供参考。

package com.test;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ComboViewerExample {

public static void main(String[] args) {
    Display display = new Display();

    Shell documentViewDialog = new Shell(Display.getDefault().getActiveShell(),SWT.SHELL_TRIM);
    documentViewDialog.setLayout(new FillLayout()); 

    Map<String , String> persons = new HashMap<String, String>();
    persons.put("1","Bob");
    persons.put("2","Christine");
    persons.put("3","Michael");

    ComboViewer viewer = new ComboViewer(documentViewDialog,SWT.BORDER);

    viewer.setContentProvider(new IStructuredContentProvider() {

        @Override
        public void inputChanged(Viewer paramViewer, Object paramObject1, Object paramObject2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void dispose() {
            // TODO Auto-generated method stub

        }

        @Override
        public Object[] getElements(Object paramObject) {

            return ((HashMap)paramObject).keySet().toArray();
        }
    });     

    viewer.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            Object obj = persons.get(element);
            String stg = element.toString() + " " + obj;
            return stg;
        }

    });



    viewer.setInput(persons);

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent paramSelectionChangedEvent) {
            ((StructuredSelection)paramSelectionChangedEvent.getSelection()).getFirstElement();
            viewer.getCombo().setText(((StructuredSelection)paramSelectionChangedEvent.getSelection()).getFirstElement().toString());
        }
    });

    documentViewDialog.setSize(150, 150);
    documentViewDialog.open();
    while(!documentViewDialog.isDisposed())
    {
        if(!display.readAndDispatch())
        {
                display.sleep();
        }
    }
    display.dispose();
}

}