2

我有一个由 JFace TableViewer 包装的 SWT 表。我知道如何右对齐整列:

TableColumn column = new TableColumn(viewer.getTable(), SWT.RIGHT);

但是,我想要做的是根据输入在每个单元格的基础上改变对齐方式 - 基本上我只想右对齐包含数字的单元格。

我一直在尝试使用一个片段(取自http://wiki.eclipse.org/JFaceSnippets#Snippet016TableLayout)。此表使用 CellLabelProvider。我想我也许可以改变方法中的对齐update(ViewerCell cell)方式,因为这种方法允许您在每个单元格的基础上设置例如背景颜色。但是,我找不到合适的东西来刺激。

有什么建议么?下面的完整片段。

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * Snippet for alignment in a table viewer.
 */
public class TableViewerAlignmentSnippet {

  private class MyContentProvider implements IStructuredContentProvider {
    public Object[] getElements(final Object inputElement) {
      return (MyModel[]) inputElement;
    }

    public void dispose() {
    }

    public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {

    }
  }

  private class MyLabelProvider extends CellLabelProvider implements ITableLabelProvider {

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

    @Override
    public String getColumnText(final Object element, final int columnIndex) {
      return element.toString();
    }

    @Override
    public void update(final ViewerCell cell) {
      // TODO - set alignment based on the cell text?
    }

  }

  public class MyModel {
    public int counter;

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

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

  public TableViewerAlignmentSnippet(final Composite comp) {
    final TableViewer v = new TableViewer(new Table(comp, SWT.BORDER));
    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());
    v.getTable().setHeaderVisible(true);

    TableColumnLayout ad = new TableColumnLayout();
    comp.setLayout(ad);

    TableColumn column = new TableColumn(v.getTable(), SWT.NONE);
    column.setText("Column 1");
    column.setMoveable(true);
    ad.setColumnData(column, new ColumnWeightData(90, 290));

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

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

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

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

    return elements;
  }

  public static void main(final String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    new TableViewerAlignmentSnippet(shell);
    shell.open();

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

    display.dispose();

  }

}
4

1 回答 1

0

我认为您应该考虑为OwnerDrawLabelProvider要设置每个单元格对齐的列绘制单元格。

于 2013-01-30T05:30:52.570 回答