Mega Code Archive

 
Categories / Java Tutorial / SWT
 

You can include the mnemonic character in the text label

Inserting an ampersand (&) before a character makes the character the mnemonic. import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ButtonAmpersandCharacter {   public static void main(String[] args) {     final Display display = new Display();     final Shell shell = new Shell(display, SWT.SHELL_TRIM);     shell.setLayout(new RowLayout());     Button button = new Button(shell, SWT.RIGHT);     button.setText("&text on the button");     shell.open();     // Set up the event loop.     while (!shell.isDisposed()) {       if (!display.readAndDispatch()) {         // If no more entries in event queue         display.sleep();       }     }     display.dispose();   } } When the user presses a key matching the mnemonic, the corresponding button is selected and a selection event is generated as a result.