Adding Find-and-Replace Functionality

It is also possible to interact with the text in a TableItem using the getText() and setText( ) methods. One such use of setText( ) and getText( ) is to build a find-and-replace capability into your tables.

Note

Although TableItem cells are not editable, you can allow the user to change data in a cell simply by providing a text field for the entry of data. In fact, this is the approach taken by many spreadsheet programs.

How do I do that?

Example 12-3 incorporates find-and-replace functionality into the TableShellExample.

Example 12-3. A Table with find-and-replace

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 = 4; 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 = 4; t.setLayoutData(gd); t.setHeaderVisible(true); final TableColumn tc1 = new TableColumn(t, SWT.LEFT); final TableColumn tc2 = new TableColumn(t, SWT.CENTER); final TableColumn ...

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.