navigate a SWT table cells with arrow keys : Table « SWT JFace Eclipse « Java






navigate a SWT table cells with arrow keys

navigate a SWT table cells with arrow keys


/*
 * TableCursor example snippet: navigate a table cells with arrow keys. 
 * Edit when user hits Return key.  Exit edit mode by hitting Escape (cancels edit)
 * or Return (applies edit to table).
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class Snippet96 {

  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 < 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 == 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 == SWT.ESC) {
              text.dispose();
            }
          }
        });
        editor.setEditor(text);
        text.setFocus();
      }
    });
    // Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key.
    // This alows the user to select multiple items in the table.
    cursor.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if (e.keyCode == SWT.CTRL || e.keyCode == SWT.SHIFT
            || (e.stateMask & SWT.CONTROL) != 0
            || (e.stateMask & SWT.SHIFT) != 0) {
          cursor.setVisible(false);
        }
      }
    });
    // Show the TableCursor when the user releases the "SHIFT" or "CTRL"
    // key.
    // This signals the end of the multiple selection task.
    table.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0)
          return;
        if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0)
          return;
        if (e.keyCode != SWT.CONTROL
            && (e.stateMask & SWT.CONTROL) != 0)
          return;
        if (e.keyCode != SWT.SHIFT && (e.stateMask & SWT.SHIFT) != 0)
          return;

        TableItem[] selection = table.getSelection();
        TableItem row = (selection.length == 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();
  }
}


           
       








Related examples in the same category

1.How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener FactoryHow to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory
2.Print KTable (SWT Table)Example Print KTable (SWT Table)Example
3.The source of a custom table class for Java SWT applicationsThe source of a custom table class for Java SWT applications
4.SWT Table Editor
5.Simple File Browser in SWT TableSimple File Browser in SWT Table
6.Bug Tracker JFaceBug Tracker JFace
7.Bug TrackerBug Tracker
8.File Browser DemoFile Browser Demo
9.TableEditor exampleTableEditor example
10.File Browser SampleFile Browser Sample
11.File Browser JFace
12.Bug ReportBug Report
13.Displays ASCII CodesDisplays ASCII Codes
14.Demonstrates the SWT.VIRTUAL styleDemonstrates the SWT.VIRTUAL style
15.Displays a tableDisplays a table
16.A table of baseball players and allows sortingA table of baseball players and allows sorting
17.Demonstrates TableCursorDemonstrates TableCursor
18.Demonstrates TableTreeDemonstrates TableTree
19.Demonstrates TableEditorDemonstrates TableEditor
20.Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
21.Table Example 3Table Example 3
22.Table Example 2Table Example 2
23.Table ExampleTable Example
24.Demonstrates TableViewersDemonstrates TableViewers
25.Demonstrates CheckboxTableViewerDemonstrates CheckboxTableViewer
26.Demonstrates CellEditorsDemonstrates CellEditors
27.SWT Table Simple DemoSWT Table Simple Demo
28.Create fake tooltips for items in a SWT tableCreate fake tooltips for items in a SWT table
29.Place a progress bar in a SWT tablePlace a progress bar in a SWT table
30.edit a cell in a SWT table (in place, fancy)edit a cell in a SWT table (in place, fancy)
31.edit the text of a SWT table item (in place)edit the text of a SWT table item (in place)
32.Update SWT table item textUpdate SWT table item text
33.Sort a SWT table by columnSort a SWT table by column
34.Select an index (select and scroll) in a SWT tableSelect an index (select and scroll) in a SWT table
35.Scroll a SWT table (set the top index)Scroll a SWT table (set the top index)
36.Resize columns as SWT table resizesResize columns as SWT table resizes
37.Remove selected items in a SWT tableRemove selected items in a SWT table
38.Print selected items in a SWT tablePrint selected items in a SWT table
39.Place arbitrary controls in a SWT tablePlace arbitrary controls in a SWT table
40.Reorder columns and reorder columns programmaticallyReorder columns and reorder columns programmatically
41.Insert a SWT table column (at an index)Insert a SWT table column (at an index)
42.Insert a SWT table item (at an index)Insert a SWT table item (at an index)
43.Find a SWT table cell from mouse down (works for any table style)Find a SWT table cell from mouse down (works for any table style)
44.Find a table cell from mouse down (SWT.FULL_SELECTION)Find a table cell from mouse down (SWT.FULL_SELECTION)
45.Detect a selection or check event in a table (SWT.CHECK)Detect a selection or check event in a table (SWT.CHECK)
46.Create a SWT table with 1,000,000 itemsCreate a SWT table with 1,000,000 items
47.Create a SWT table (columns, headers, lines)Create a SWT table (columns, headers, lines)
48.Create a SWT table (no columns, no headers)Create a SWT table (no columns, no headers)
49.Color cells and rows in SWT tableColor cells and rows in SWT table
50.Create a virtual SWT table and add 1000 entries to it every 500 msCreate a virtual SWT table and add 1000 entries to it every 500 ms
51.Dropped data type depends on target item in tableDropped data type depends on target item in table
52.Create a table (lazy)Create a table (lazy)