In our Eclipse RCP 3.7 application, we download Excel files and display the first 30 rows in the table, the problem with rendering is not in the rows, but in the number of columns, when it reaches a certain size, the table does not look right, and when you scroll to the right, cells and headers fail, and all this just goes crazy.
The separate class below demonstrates this problem and follows the main pattern that I used in my application in case the problem occurs.
Any thoughts on how to solve this problem?
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); } } }
source share