0

我使用了 org.eclipse.swt.widgets.Table 代码实现会是这个样子

表 table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);

输出看起来像这样

但是我需要像鼠标单击“Sec_1”这样的行为,我只想选择“Sec_1”而不是整行,如果鼠标单击“First_1”,我不希望它被突出显示(FirstColumn 没有选择)。谁能帮我 ?

4

1 回答 1

1

请查看此代码片段,例如(http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/ eclipse/jface/snippets/viewers/Snippet036FocusBorderCellHighlighter.java):

public class Test {

private class MyContentProvider implements IStructuredContentProvider {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {
        return (MyModel[]) inputElement;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
     *      java.lang.Object, java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

}

public static boolean flag = true;

public class MyModel {
    public int counter;

    public MyModel(int counter) {
        this.counter = counter;
    }

    public String toString() {
        return "Item " + this.counter;
    }
}

public class MyLabelProvider extends LabelProvider implements
        ITableLabelProvider, ITableFontProvider, ITableColorProvider {
    FontRegistry registry = new FontRegistry();

    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        return "Column " + columnIndex + " => " + element.toString();
    }

    public Font getFont(Object element, int columnIndex) {
        return null;
    }

    public Color getBackground(Object element, int columnIndex) {
        return null;
    }

    public Color getForeground(Object element, int columnIndex) {
        return null;
    }

}

public Test(Shell shell) {
    final TableViewer v = new TableViewer(shell, SWT.BORDER|SWT.FULL_SELECTION);
    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());

    v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTable()), new TextCellEditor(v.getTable()), new TextCellEditor(v.getTable()) });
    v.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            return true;
        }

        public Object getValue(Object element, String property) {
            return "Column " + property + " => " + element.toString();
        }

        public void modify(Object element, String property, Object value) {

        }

    });

    v.setColumnProperties(new String[] {"1","2","3"});

    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v));
    ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
        protected boolean isEditorActivationEvent(
                ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };

    TableViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
            | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
            | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);


    TableColumn column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 1");

    column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 2");

    column = new TableColumn(v.getTable(), SWT.NONE);
    column.setWidth(200);
    column.setMoveable(true);
    column.setText("Column 3");


    MyModel[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);
    v.getTable().setHeaderVisible(true);
}

private MyModel[] createModel() {
    MyModel[] elements = new MyModel[10];

    for (int i = 0; i < 10; i++) {
        elements[i] = new MyModel(i);
    }

    return elements;
}

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

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Test(shell);
    shell.open();

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

    display.dispose();

}

}
于 2013-07-24T07:44:29.760 回答