Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2008, 05:07 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to navigate a SWT table cells with arrow keys
Code:
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(); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
merging selected table cells using javascript Renjini JavaServer Pages (JSP) and JSTL 2 06-17-2008 03:04 AM
Retreiving list of keys from Map with certan value desmond5 New To Java 1 03-06-2008 10:10 PM
how to draw an arrow mark using java swing sandhyau AWT / Swing 5 02-07-2008 01:52 PM
Draw an arrow Albert SWT / JFace 3 02-01-2008 10:11 AM
no of fonts and cells exceeding error in excel sheet. iimasd AWT / Swing 0 11-06-2007 08:58 AM


All times are GMT +3. The time now is 07:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org