
JFace TableViewer and MigLayout - Scrollbars
Hello! I am quite fond of MigLayout and I use it in my RCP project to create forms and the like. I also used it for SWT Composites containing a JFace TableViewer or JFace TreeViewer that scroll in both directions (SWT.H_SCROLL | SWT.V_SCROLL). Unfortunately I did not manage to configure MigLayout in a way that scrollbars are shown when I resize the Window (a Shell or RCP View, does not matter) so that the table is larger than the window (in which case scrollbars should appear; note: Scrollbars appear whenever the Table's contents are larger than the Tables size; obviously the table's size is not reduced according to the reduction in size of the window).
The following runnable snippet demonstrates the problem (note: you'll need SWT and JFace libraries, for example obtained by creating a RCP project with eclipse). When I use a normal FillLayout or GridLayout everything works fine. But how do I have to configure the MigLayout to achieve the same result (see lines 67/68)? Thanks for your help
Hendrik
Code:
package myrcpdemo;
import net.miginfocom.swt.MigLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellLabelProvider;
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.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
public class SomeClass {
private final TableViewer tableViewer;
public SomeClass(final Shell shell) {
tableViewer = new TableViewer(shell, SWT.V_SCROLL | SWT.H_SCROLL
| SWT.BORDER);
final CellLabelProvider labelProvider = new CellLabelProvider() {
@Override
public void update(final ViewerCell cell) {
final String text = ((MyModel) cell.getElement()).getText(cell
.getColumnIndex());
cell.setText(text);
}
};
for (int i = 0; i < 10; i++) {
final TableColumn column = new TableColumn(tableViewer.getTable(), SWT.NONE);
column.setResizable(true);
column.setText("Column " + i);
column.setWidth(150);
final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, column);
viewerColumn.setLabelProvider(labelProvider);
}
tableViewer.setContentProvider(new ArrayContentProvider());
final MyModel[] elements = new MyModel[10];
for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i);
}
tableViewer.setInput(elements);
tableViewer.getTable().setLinesVisible(true);
}
public TableViewer getTableViewer() {
return tableViewer;
}
public class MyModel {
public int counter;
public MyModel(final int counter) {
this.counter = counter;
}
public String getText(final int columnIndex) {
return "Item " + counter + " - Column " + columnIndex;
}
}
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
// shell.setLayout(new FillLayout());
shell.setLayout(new MigLayout());
new SomeClass(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}