TableCursor

A TableCursor provides a way for the user to navigate around a Table using the keyboard. It also provides a mechanism for selecting an individual cell in a table.

<p> Here is an example of using a TableCursor to navigate to a cell and then edit it.

<code><pre> public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout());

// create a a table with 3 columns and fill with data final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); table.setLayoutData(new GridData(GridData.FILL_BOTH)); TableColumn column1 = new TableColumn(table, SWT.NONE); TableColumn column2 = new TableColumn(table, SWT.NONE); TableColumn column3 = new TableColumn(table, SWT.NONE); for (int i = 0; i &lt; 100; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "cell "+i+" 0", "cell "+i+" 1", "cell "+i+" 2"}); } column1.pack(); column2.pack(); column3.pack();

// create a TableCursor to navigate around the table final TableCursor cursor = new TableCursor(table, SWT.NONE); // create an editor to edit the cell when the user hits "ENTER" // while over a cell in the table final ControlEditor editor = new ControlEditor(cursor); editor.grabHorizontal = true; editor.grabVertical = true;

cursor.addSelectionListener(new SelectionAdapter() { // when the TableEditor is over a cell, select the corresponding row in // the table public void widgetSelected(SelectionEvent e) { table.setSelection(new TableItem[] {cursor.getRow()}); } // when the user hits "ENTER" in the TableCursor, pop up a text editor so that // they can change the text of the cell public void widgetDefaultSelected(SelectionEvent e){ final Text text = new Text(cursor, SWT.NONE); TableItem row = cursor.getRow(); int column = cursor.getColumn(); text.setText(row.getText(column)); text.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { // close the text editor and copy the data over // when the user hits "ENTER" if (e.character is SWT.CR) { TableItem row = cursor.getRow(); int column = cursor.getColumn(); row.setText(column, text.getText()); text.dispose(); } // close the text editor when the user hits "ESC" if (e.character is SWT.ESC) { text.dispose(); } } }); editor.setEditor(text); text.setFocus(); } }); // Hide the TableCursor when the user hits the "MOD1" or "MOD2" key. // This allows the user to select multiple items in the table. cursor.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.keyCode is SWT.MOD1 || e.keyCode is SWT.MOD2 || (e.stateMask & SWT.MOD1) !is 0 || (e.stateMask & SWT.MOD2) !is 0) { cursor.setVisible(false); } } }); // Show the TableCursor when the user releases the "MOD2" or "MOD1" key. // This signals the end of the multiple selection task. table.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { if (e.keyCode is SWT.MOD1 && (e.stateMask & SWT.MOD2) !is 0) return; if (e.keyCode is SWT.MOD2 && (e.stateMask & SWT.MOD1) !is 0) return; if (e.keyCode !is SWT.MOD1 && (e.stateMask & SWT.MOD1) !is 0) return; if (e.keyCode !is SWT.MOD2 && (e.stateMask & SWT.MOD2) !is 0) return;

TableItem[] selection = table.getSelection(); TableItem row = (selection.length is 0) ? table.getItem(table.getTopIndex()) : selection[0]; table.showItem(row); cursor.setSelection(row, 0); cursor.setVisible(true); cursor.setFocus(); } });

shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } </pre></code>

<dl> <dt><b>Styles:</b></dt> <dd>BORDER</dd> <dt><b>Events:</b></dt> <dd>Selection, DefaultSelection</dd> </dl>

@since 2.0

@see <a href="http://www.eclipse.org/swt/snippets/#tablecursor">TableCursor snippets</a> @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>

Constructors

this
this(Table parent, int style)

Constructs a new instance of this class given its parent table and a style value describing its behavior and appearance. <p> The style value is either one of the style constants defined in class <code>SWT</code> which is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses. </p>

Members

Functions

addSelectionListener
void addSelectionListener(SelectionListener listener)

Adds the listener to the collection of listeners who will be notified when the user changes the receiver's selection, by sending it one of the messages defined in the <code>SelectionListener</code> interface. <p> When <code>widgetSelected</code> is called, the item field of the event object is valid. If the receiver has <code>SWT.CHECK</code> style set and the check selection changes, the event object detail field contains the value <code>SWT.CHECK</code>. <code>widgetDefaultSelected</code> is typically called when an item is double-clicked. </p>

getBackground
Color getBackground()

Returns the background color that the receiver will use to draw.

getColumn
int getColumn()

Returns the column over which the TableCursor is positioned.

getForeground
Color getForeground()

Returns the foreground color that the receiver will use to draw.

getRow
TableItem getRow()

Returns the row over which the TableCursor is positioned.

removeSelectionListener
void removeSelectionListener(SelectionListener listener)

Removes the listener from the collection of listeners who will be notified when the user changes the receiver's selection.

setBackground
void setBackground(Color color)

Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null. <p> Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed. </p> @param color the new color (or null)

setForeground
void setForeground(Color color)

Sets the receiver's foreground color to the color specified by the argument, or to the default system color for the control if the argument is null. <p> Note: This operation is a hint and may be overridden by the platform. </p> @param color the new color (or null)

setSelection
void setSelection(int row, int column)

Positions the TableCursor over the cell at the given row and column in the parent table.

setSelection
void setSelection(TableItem row, int column)

Positions the TableCursor over the cell at the given row and column in the parent table.

Inherited Members

From Canvas

drawBackground
void drawBackground(GC gc, int x, int y, int width, int height)

Fills the interior of the rectangle specified by the arguments, with the receiver's background.

getCaret
Caret getCaret()

Returns the caret. <p> The caret for the control is automatically hidden and shown when the control is painted or resized, when focus is gained or lost and when an the control is scrolled. To avoid drawing on top of the caret, the programmer must hide and show the caret when drawing in the window any other time. </p>

getIME
IME getIME()

Returns the IME.

scroll
void scroll(int destX, int destY, int x, int y, int width, int height, bool all)

Scrolls a rectangular area of the receiver by first copying the source area to the destination and then causing the area of the source which is not covered by the destination to be repainted. Children that intersect the rectangle are optionally moved during the operation. In addition, outstanding paint events are flushed before the source area is copied to ensure that the contents of the canvas are drawn correctly.

setCaret
void setCaret(Caret caret)

Sets the receiver's caret. <p> The caret for the control is automatically hidden and shown when the control is painted or resized, when focus is gained or lost and when an the control is scrolled. To avoid drawing on top of the caret, the programmer must hide and show the caret when drawing in the window any other time. </p> @param caret the new caret for the receiver, may be null

setIME
void setIME(IME ime)

Sets the receiver's IME.

Meta