Java Tutorial/SWT/SWT Event
Содержание
- 1 Add addPaintListener to Composite
- 2 Add and remove filter listeners
- 3 Add default selection listener to Combo
- 4 Adding AccessibleControlListener
- 5 Adding AccessibleListener
- 6 Add Resize listener to Shell
- 7 Demonstrate DisposeListener which is notified on the associated widget"s disposal
- 8 Events
- 9 Get event type
- 10 implements general Event Listener
- 11 Introducing Typed Listeners
- 12 Introducing Untyped Listeners
- 13 SWT Message Keys and Values
- 14 SWT provides two kinds of event listening mechanism: typed and untyped.
- 15 The Event Model
- 16 The listener notification process
- 17 Typed Listeners
- 18 Untyped Events and Untyped Event Listeners
- 19 Using ControlListener
- 20 Using FocusListener
- 21 Using HelpListener
- 22 Using ModifyListener
- 23 Using SelectionListener
- 24 Using VerifyListener
Add addPaintListener to Composite
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompositePaintListener {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL);
final Composite composite = new Composite (shell, SWT.BORDER);
composite.setSize (700, 600);
final Color red = display.getSystemColor (SWT.COLOR_RED);
composite.addPaintListener (new PaintListener() {
public void paintControl (PaintEvent e) {
e.gc.setBackground (red);
e.gc.fillOval (5, 5, 690, 590);
}
});
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Add and remove filter listeners
public void addFilter(int eventType, Listener listener)
public void removeFilter(int eventType, Listener listener)
Add default selection listener to Combo
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ComboSelectionListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Combo combo = new Combo(shell, SWT.NONE);
combo.setItems(new String[] { "A-1", "B-1", "C-1" });
combo.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
System.out.println(e.widget + " - Default Selection");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Adding AccessibleControlListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.Accessible;
import org.eclipse.swt.accessibility.AccessibleControlAdapter;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class AccessibleControlListenerAdding {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("asdf");
Accessible accessible = label.getAccessible();
accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
public void getState(AccessibleControlEvent e) {
super.getState(e);
System.out.println("AccessibleControlListener");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Adding AccessibleListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.Accessible;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class AccessibleListenerAdding {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("asdf");
Accessible accessible = label.getAccessible();
accessible.addAccessibleListener(new AccessibleAdapter() {
public void getName(AccessibleEvent e) {
super.getName(e);
e.result = "Speak this instead of the text";
System.out.println("Accessible");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Add Resize listener to Shell
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ShellResizeEvent {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL);
shell.addListener (SWT.Resize, new Listener () {
public void handleEvent (Event e) {
Rectangle rect = shell.getClientArea ();
System.out.println(rect);
}
});
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Demonstrate DisposeListener which is notified on the associated widget"s disposal
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DisposeListenerWithShell {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
// Create the child shell and the dispose listener
final Shell childShell = new Shell(shell);
childShell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
// When the child shell is disposed, change the message on the main shell
System.out.println("Dispoase");
}
});
childShell.setLayout(new FillLayout());
childShell.setText("little brother");
childShell.open();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Events
- SWT offers two types of listeners: untyped and typed.
- Untyped listeners can lead to smaller code.
- Typed listeners lead to more modular designs.
Get event type
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class EventTypeGet {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("Click and check the console");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
System.out.println(getEventName(e.type));
switch (e.type) {
case SWT.Selection:
System.out.println("Button pressed");
break;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static String getEventName(int eventType) {
switch(eventType) {
case SWT.None:
return "null";
case SWT.KeyDown:
return "key down";
case SWT.KeyUp:
return "key up";
case SWT.MouseDown:
return "mouse down";
case SWT.MouseUp:
return "mouse up";
case SWT.MouseMove:
return "mouse move";
case SWT.MouseEnter:
return "mouse enter";
case SWT.MouseExit:
return "mouse exit";
case SWT.MouseDoubleClick:
return "mouse double click";
case SWT.Paint:
return "paint";
case SWT.Move:
return "move";
case SWT.Resize:
return "resize";
case SWT.Dispose:
return "dispose";
case SWT.Selection:
return "selection";
case SWT.DefaultSelection:
return "default selection";
case SWT.FocusIn:
return "focus in";
case SWT.FocusOut:
return "focus out";
case SWT.Expand:
return "expand";
case SWT.Collapse:
return "collapse";
case SWT.Iconify:
return "iconify";
case SWT.Deiconify:
return "deiconify";
case SWT.Close:
return "close";
case SWT.Show:
return "show";
case SWT.Hide:
return "hide";
case SWT.Modify:
return "modify";
case SWT.Verify:
return "verify";
case SWT.Activate:
return "activate";
case SWT.Deactivate:
return "deactivate";
case SWT.Help:
return "help";
case SWT.DragDetect:
return "drag detect";
case SWT.Arm:
return "arm";
case SWT.Traverse:
return "traverse";
case SWT.MouseHover:
return "mouse hover";
case SWT.HardKeyDown:
return "hard key down";
case SWT.HardKeyUp:
return "hard key up";
case SWT.MenuDetect:
return "menu detect";
}
return "unkown ???";
}
}
implements general Event Listener
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class EventListenerGeneral {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);
shell.setLayout(new GridLayout());
Listener listener = new MouseEnterExitListener();
label.setText("Point your cursor here ...");
label.setBounds(30, 30, 200, 30);
label.addListener(SWT.MouseEnter, listener);
label.addListener(SWT.MouseExit, listener);
shell.setSize(260, 120);
shell.open();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
class MouseEnterExitListener implements Listener {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseEnter:
System.out.println("Cursor enters the label");
break;
case SWT.MouseExit:
System.out.println("Cursor leaves the label");
break;
}
}
}
Introducing Typed Listeners
- Typed listeners use classes and interfaces specific to each possible event.
- For example, to listen for a button click, register a SelectionListener implementation with the button using the button"s addSelectionListener() method.
- All typed events ultimately derive from a common class: TypedEvent.
- Many event classes have a boolean member called doit that you can set to false to cancel the processing of that event.
MemberDescriptionObject dataContains application-specific dataDisplay displayThe display where the event occurredint timeThe time at which the event occurredWidget widgetThe source of the event
Introducing Untyped Listeners
- The untyped listener interface is represented by the Listener interface
- It contains one method: void handleEvent(Event event)
Event Members
MemberDescriptionint buttonThe one-based index of the button that was clicked or released.char characterThe character that was typed.int countThe number of pending paint events.Object dataApplication-specific data.int detailA detail constant from the SWT class that contains details about the event.Display displayThe display where the event occurred.boolean doitA flag indicating whether to process this event. Not supported for all events.int endThe end of the range of modified text.GC gcThe graphics context associated with this event.int heightThe height in pixels of the rectangle that needs painting.Widget itemThe widget where the event occurred.int keyCodeThe key code of the key that was typed.int startThe beginning of the range of modified text.int stateMaskThe mask describing the state of the modifier keys at the time of the event.String textThe text to insert.int timeThe event"s time.int typeThe type of the event. This is the field to switch on to handle the various event types.Widget widgetThe widget that issued the event.int widthThe width in pixels of the rectangle that needs painting.int xEither the x offset of the rectangle that needs painting or the x coordinate of the mouse pointer at the time of the event, depending n the event.int yEither the y offset of the rectangle that needs painting or the y coordinate of the mouse pointer at the time of the event, depending on the event.
To add an untyped listener to a widget, call addListener() on it.
void addListener(int eventType, Listener listener)
eventType contains one of the event type constants from the SWT class:
TypeDescriptionSWT.ActivateTriggered when the widget becomes the active windowSWT.ArmTriggered when the widget is armedSWT.CloseTriggered when the widget is closedSWT.CollapseTriggered when a tree node is collapsedSWT.DeactivateTriggered when the widget is no longer the active windowSWT.DefaultSelectionTriggered when the default selection occursSWT.DeiconifyTriggered when the widget is restored from being minimizedSWT.DisposeTriggered when the widget is disposedSWT.DragDetectTriggered when the widget is draggedSWT.ExpandTriggered when a tree node is expandedSWT.FocusInTriggered when the widget gains focusSWT.FocusOutTriggered when the widget loses focusSWT.HardKeyDownTriggered when a special hardware key, such as on a Pocket PC device, is pressedSWT.HardKeyUpTriggered when a special hardware key, such as on a Pocket PC device, is releasedSWT.HelpTriggered when the user requests helpSWT.HideTriggered when the widget is hiddenSWT.IconifyTriggered when the widget is minimizedSWT.KeyDownTriggered when the user presses a keySWT.KeyUpTriggered when the user releases a keySWT.MenuDetectTriggered when a menu is selectedSWT.ModifyTriggered when the text of a widget is modifiedSWT.MouseDoubleClickTriggered when the mouse is double-clickedSWT.MouseDownTriggered when the mouse button is clickedSWT.MouseEnterTriggered when the mouse pointer enters the widgetSWT.MouseExitTriggered when the mouse pointer exits the widgetSWT.MouseHoverTriggered when the mouse pointer hovers over the widgetSWT.MouseMoveTriggered when the mouse pointer moves through the widgetSWT.MouseUpTriggered when the mouse button is releasedSWT.MoveTriggered when the widget is movedSWT.NoneNull eventSWT.PaintTriggered when the widget is paintedSWT.ResizeTriggered when the widget is resizedSWT.SelectionTriggered when the widget is selectedSWT.ShowTriggered when the widget is shownSWT.TraverseTriggered when the user tabs through the controlsSWT.VerifyTriggered when the text for the widget is about to change, allowing you to veto the change
SWT Message Keys and Values
KeyValueSWT_YesYesSWT_NoNoSWT_OKOKSWT_CancelCancelSWT_AbortAbortSWT_RetryRetrySWT_IgnoreIgnoreSWT_SampleSampleSWT_A_Sample_TextA Sample TextSWT_SelectionSelectionSWT_Current_SelectionCurrent SelectionSWT_FontFontSWT_ColorColorSWT_Extended_styleExtended styleSWT_SizeSizeSWT_StyleStyleSWT_SaveSaveSWT_Character_setCharacter setSWT_ColorDialog_TitleColorsSWT_FontDialog_TitleFontsSWT_Charset_WesternWesternSWT_Charset_EastEuropeanEast EuropeanSWT_Charset_SouthEuropeanSouth EuropeanSWT_Charset_NorthEuropeanNorth EuropeanSWT_Charset_CyrillicCyrillicSWT_Charset_ArabicArabicSWT_Charset_GreekGreekSWT_Charset_HebrewHebrewSWT_Charset_TurkishTurkishSWT_Charset_NordicNordicSWT_Charset_ThaiThaiSWT_Charset_BalticRimBaltic RimSWT_Charset_CelticCelticSWT_Charset_EuroEuroSWT_Charset_RomanianRomanianSWT_Charset_SimplifiedChineseSimplified ChineseSWT_Charset_TraditionalChineseTraditional ChineseSWT_Charset_JapaneseJapaneseSWT_Charset_KoreanKoreanSWT_Charset_UnicodeUnicodeSWT_Charset_ASCIIASCIISWT_InputMethodsInput Methods
SWT provides two kinds of event listening mechanism: typed and untyped.
A typed listener can be used to listen for only one particular typed event. For example, SelectionListener is a typed listener for event SelectionEvent. Untyped event listeners offer a generic, low-level mechanism to listen for events.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GenericEventUntyped {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("Click and check the console");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
System.out.println("widgetSelected");
}
public void widgetDefaultSelected(SelectionEvent arg0) {
System.out.println("widgetDefaultSelected");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
The Event Model
- SWT uses the observer design pattern based event model.
- Multiple listeners can be registered to be notified.
- In SWT, only Widgets and Displays can have event listeners.
The following code demonstrates usage of a listener:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class EventModelPattern {
static Display display = new Display();
public static void main(String[] args) {
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("push me");
button.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
System.out.println("Button pushed.");
}
});
shell.open();
while (!shell.isDisposed()) { // Event loop.
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
The listener notification process
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class DisplayFilterEvent {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Left click your mouse");
shell.setSize(200, 100);
shell.open();
shell.addListener(SWT.MouseDown, new SimpleListener("Shell mouse down listener"));
display.addFilter(SWT.MouseDown, new SimpleListener("Display mouse down Listener"));
display.addFilter(SWT.MouseUp, new SimpleListener("Display mouse up Listener"));
shell.open();
while (!shell.isDisposed()) { // Event loop.
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
class SimpleListener implements Listener {
String name;
public SimpleListener(String name) {
this.name = name;
}
public void handleEvent(Event e) {
System.out.println("Event: [" + e.toString() + "] from " + name
+ ". \tCurrent Time (in ms): " + System.currentTimeMillis());
}
}
Typed Listeners
SWT provides implementations of every listener interface that has more than one method. The names of these classes end in Adapter.
Listener Description Event Adapter
ArmListener Listens for arm events ArmEvent None
ControlListener Listens for move and resize events ControlEvent ControlAdapter
DisposeListener Listens for dispose events DisposeEvent None
FocusListener Listens for focus gained and FocusEvent FocusAdapter
lost events
HelpListener Listens for help requests HelpEvent None
KeyListener Listens for key presses and KeyEvent KeyAdapter
releases
MenuListener Listens for menu events MenuEvent MenuAdapter
ModifyListener Listens for text modifications ModifyEvent None
MouseListener Listens for mouse button presses MouseEvent MouseAdapter
MouseMoveListener Listens for mouse movements MouseEvent None
MouseTrackListener Listens for when the mouse MouseEvent MouseTrackAdapter
enters, exits, or hovers over a control
PaintListener Listens for paint events PaintEvent None
SelectionListener Listens for selection events SelectionEvent SelectionAdapter
(for example, button clicks)
ShellListener Listens for shell events ShellEvent ShellAdapter
TraverseListener Listens for traverse events TraverseEvent None
TreeListener Listens for tree events TreeEvent TreeAdapter
VerifyListener Listens for, and potentially VerifyEvent None
intercepts, text modifications
Untyped Events and Untyped Event Listeners
An untyped event listener can be registered to listen for any type of event. SWT has two classes for untyped event:
- an interface Listener.
- an event class named Event.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class UntypedEventListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("Click and check the console");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
System.out.println(e.button);
switch (e.type) {
case SWT.Selection:
System.out.println("Button pressed");
break;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Using ControlListener
A ControlListener listens for resize or move events.
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent event) {
Shell shell = (Shell) event.getSource();
Rectangle rect = shell.getClientArea();
System.out.println(rect);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Using FocusListener
FocusListener is informed when a control gains or loses the focus.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FocusListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
shell.setText("One Potato, Two Potato");
// Create the focus listener
FocusListener listener = new FocusListener() {
public void focusGained(FocusEvent event) {
Button button = (Button) event.getSource();
button.setText("I"m It!");
}
public void focusLost(FocusEvent event) {
Button button = (Button) event.getSource();
button.setText("Pick Me!");
}
};
// Create the buttons and add the listener to each one
for (int i = 0; i < 6; i++) {
Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
button.setText("Pick Me!");
button.addFocusListener(listener);
}
// Display the window
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Using HelpListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class HelpListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setText("press F1 function to see the help message.");
text.addHelpListener(new HelpListener() {
public void helpRequested(HelpEvent arg0) {
System.out.println("Help wanted");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Using ModifyListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ModifyListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setText("Type somthing to modify.");
text.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent arg0) {
System.out.println("Text modified");
}});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Using SelectionListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonSelectionListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.NONE);
button.setText("Click and check the console");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("selection event");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Using VerifyListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class VerifyListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setText("Type somthing to verify.");
text.addVerifyListener(new VerifyListener(){
public void verifyText(VerifyEvent arg0) {
System.out.println("verifying");
}});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}