在我们的 Eclipse RCP 3.7 应用程序中,我们加载 Excel 文件并在表格中显示前 30 行,渲染的问题不在于行数,而在于列数,当表格达到一定大小时,表格不会t 似乎画得很好,当你向右滚动时,单元格和标题不对齐,整个事情变得疯狂。
下面的独立类演示了这个问题,并遵循我在应用程序中使用的基本模式,以防出现问题。
关于如何解决这个问题的任何想法?
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class Snippet {
public static TableViewer viewer;
public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
viewer = new TableViewer(shell, SWT.FULL_SELECTION | SWT.H_SCROLL
| SWT.V_SCROLL);
Table table = viewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
createColumns(300);
viewer.setInput(generateData(300, 30));
for (TableColumn c : viewer.getTable().getColumns()) {
c.pack();
}
shell.setSize( 300, 500 );
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() ) {
display.sleep();
}
}
display.dispose();
}
private static void createColumns(int columnCount) {
for (int i = 0; i < columnCount; i++) {
createTableViewerColumn(i);
}
}
private static TableViewerColumn createTableViewerColumn(int colNum) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText("Table Header " + colNum);
column.setWidth(100);
column.setResizable(true);
column.setMoveable(false);
viewerColumn.setLabelProvider(new Snippet.SampleDataTableCellProvider(colNum));
return viewerColumn;
}
private static List<String[]> generateData(int cols, int rows) {
ArrayList<String[]> result = new ArrayList<String[]>();
for (int y = 0; y < rows; y++) {
String[] row = new String[cols];
for (int x = 0; x < cols; x++) {
row[x] = "Big long string to test how long these columns can go before it all goes mental.";
}
result.add(row);
}
return result;
}
private static class SampleDataTableCellProvider extends StyledCellLabelProvider {
private int colnumber;
private SampleDataTableCellProvider(int colnumber) {
this.colnumber = colnumber;
}
@Override
public void update(ViewerCell cell) {
String[] row = (String[]) cell.getElement();
if (row.length > colnumber) {
cell.setText(row[colnumber]);
} else {
cell.setText("");
}
super.update(cell);
}
}
}