我有一个由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();
}
}
答案 0 :(得分:0)
我认为您应该查看OwnerDrawLabelProvider
为要为每个单元格设置对齐的列绘制单元格。