Creating a Searchable Table

Although the Table class has a built-in ability to find items by typing the first character of the text in a cell, this falls far short of providing a full searchable table. Some interface designs call for the ability of the user to specify search text (using a text field) and have the table searched for matching items (perhaps by clicking a button). Providing this functionality for your users is fairly easy.

How do I do that?

This version of TableShellExample, shown in Example 12-2, adds in the ability to search a table based upon user-supplied search text.

Example 12-2. A searchable table

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; public class TableShellExample { Display d; Shell s; TableShellExample( ) { d = new Display( ); s = new Shell(d); s.setSize(250,200); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Table Shell Example"); GridLayout gl = new GridLayout( ); gl.numColumns = 2; s.setLayout(gl); final Table t = new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION); final GridData gd = new GridData(GridData.FILL_BOTH); gd.horizontalSpan = 2; t.setLayoutData(gd); t.setHeaderVisible(true); final TableColumn tc1 = new TableColumn(t, SWT.LEFT); final TableColumn tc2 = new TableColumn(t, SWT.CENTER); ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.