Eclipse Corner Article |
Summary
This article contains several examples that show how to use the features of the Eclipse Widget toolkit, SWT. Each example builds upon the ones before. The first example shows how to create an empty window. The second example adds some buttons to the window. The third example registers a listener for events on one of the buttons.
The examples below show how to use SWT to create stand-alone applications, and provide a good basis for experimenting with SWT. The Eclipse Workbench defines the UI paradigm and how editors and views are integrated. It provides the display and shell that are created below so you don't need to create them for you Workbench plug-in user interfaces.
For further details on SWT, take a look at the widget hierarchy and supported style bits. Then try modifying the example below with different widgets, styles, and actions.
// Create a shell. Display display = new Display(); // The "parent" of a shell is the display it is created on. Shell shell = new Shell (display); // Create the top level window. shell.open (); // Open it // This is the event loop. while (!shell.isDisposed ()) if (!display.readAndDispatch ()) display.sleep ();
Display display = new Display(); Shell shell = new Shell (display); Button b1 = new Button (shell, SWT.PUSH); b1.setText ("Button 1"); b1.setBounds (0, 0, 100, 60); Button b2 = new Button (shell, SWT.CHECK); b2.setText ("Button 2"); b2.setBounds (0, 60, 100, 45); shell.open (); while (!shell.isDisposed ()) if (!display.readAndDispatch ()) display.sleep ();
Shell shell = new Shell (); Button b1 = new Button (shell, SWT.PUSH); b1.setText ("Button 1"); b1.setBounds (0, 0, 100, 60); b1.addMouseListener(new MouseListener() { public void mouseDown(MouseEvent e) { System.out.println("mouseDown:"); }; public void mouseDoubleClick(MouseEvent e) { System.out.println("mouseDoubleClick:"); }; public void mouseUp(MouseEvent e) { System.out.println("mouseUp:"); }; }); Button b2 = new Button (shell, SWT.CHECK); b2.setText ("Button 2"); b2.setBounds (0, 60, 100, 45); shell.open (); Display display = shell.getDisplay (); while (!shell.isDisposed ()) if (!display.readAndDispatch ()) display.sleep ();