Mohamed Iqzas Mohamed Iqzas - 1 year ago 105
Java Question

SWT - Making a table scrollable

I have tried the below code, and the table is appearing without any scrollbar. Kindly help in making the SWT table scrollable by both horizontally and vertically.

package snippet;

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet {

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

createUI(shell);


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

private static void createUI(Shell shell) {
// TODO Auto-generated method stub
GridLayout layout = new GridLayout(1, false);


shell.setLayout(layout);


Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK|SWT.RESIZE|SWT.SCROLL_PAGE);
GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_table.heightHint = 136;
gd_table.widthHint = 205;
table.setLayoutData(gd_table);
table.setHeaderVisible(true);
table.setLinesVisible(true);

TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
tblclmnNewColumn.setWidth(37);

TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_1.setWidth(219);
tblclmnNewColumn_1.setText("Item ID");

TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_2.setWidth(246);
tblclmnNewColumn_2.setText("Revision ID");

TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_3.setWidth(246);
tblclmnNewColumn_3.setText("Vendor Name");

TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_4.setWidth(246);
tblclmnNewColumn_4.setText("Vendor ID");

for(int i=0;i<10;i++)
{
TableItem item = new TableItem(table, SWT.NONE);

item.setText(1,"item_id"+i);
item.setText(2,"item_revision_id"+i);
item.setText(3,"object_string"+i);
}

}

}


I have tried to use both SWT.SCROLL_PAGE and SWT.SCROLL_LINE. both are giving the same result in this example. I am missing a basic understanding related to scrollable widgets in SWT. Kindly help in this regard.

Answer Source

The values that you're looking for are SWT.V_SCROLL and SWT.H_SCROLL. If you replace the SWT.SCROLL_PAGE constant in the Table constructor with those two, your code will work as expected.

Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK | SWT.RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download