我们如何使用jface更改swt中表行的文本

时间:2014-09-01 06:01:29

标签: java swt jface

我需要在tableitem中显示消息时更改表项的文本。所以我们如何使用jface来完成。该表由2个coloumns组成。请帮助解决这个问题,因为我是jface的新手。 / p>

1 个答案:

答案 0 :(得分:1)

好的,这是一个非常基本的例子。您必须自己处理String颜色表示与实际Color之间的映射:

private static Map<String, Integer> COLOR_MAPPING = new HashMap<>();

static
{
    COLOR_MAPPING.put("red", SWT.COLOR_RED);
    COLOR_MAPPING.put("blue", SWT.COLOR_BLUE);
}

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    TableViewer viewer = new TableViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);

    createColumns(viewer);

    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    List<MyObject> data = new ArrayList<>();
    data.add(new MyObject("Something", "red"));
    data.add(new MyObject("Something else", "blue"));
    data.add(new MyObject("Something else entirely", "bleen"));

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setInput(data);

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

private static void createColumns(TableViewer viewer)
{
    TableViewerColumn first = new TableViewerColumn(viewer, SWT.NONE);
    first.getColumn().setWidth(200);
    first.getColumn().setText("First");
    first.setLabelProvider(new ColumnLabelProvider()
    {
        @Override
        public void update(ViewerCell cell)
        {
            Object element = cell.getElement();
            if(element instanceof MyObject)
            {
                MyObject obj = (MyObject) element;
                cell.setText(obj.getFirst());
            }
        }
    });

    TableViewerColumn second = new TableViewerColumn(viewer, SWT.NONE);
    second.getColumn().setWidth(200);
    second.getColumn().setText("Second");
    second.setLabelProvider(new ColumnLabelProvider()
    {
        @Override
        public void update(ViewerCell cell)
        {
            Object element = cell.getElement();
            if(element instanceof MyObject)
            {
                MyObject obj = (MyObject) element;

                cell.setText(obj.getSecond());

                Integer colorCode = COLOR_MAPPING.get(obj.getSecond());

                if(colorCode != null)
                        cell.setForeground(Display.getDefault().getSystemColor(colorCode));
            }
        }
    });
}

private static class MyObject
{
    private String first;
    private String second;

    public MyObject(String first, String second)
    {
        this.first = first;
        this.second = second;
    }

    public String getFirst()
    {
        return first;
    }

    public void setFirst(String first)
    {
        this.first = first;
    }

    public String getSecond()
    {
        return second;
    }

    public void setSecond(String second)
    {
        this.second = second;
    }
}

看起来像这样:

enter image description here