Friday 9 December 2016

SWT: Focus listeners

There are two types of focus listeners.
a.   FocusListener
b.   TraverseListener

Focus events occur, whenever a widget receives focus. A TraverseEvent is generated when a widget gains focus by means of traversal.

FocusListener
Focus events occur, whenever a widget receives focus. FocusListener interface provides following methods.

Method
Description
public void focusGained(FocusEvent e)
Sent when a control gets focus
public void focusLost(FocusEvent e)
Sent when a control loses focus
TraverseListener
The TraverseListener has one method, keyTraversed. This is called when the widget the handler is attached to is traversed by a traversal method, such as tabs or up and down arrows. 

TraverseListener interface provide following method.
Method
Description
public void keyTraversed(TraverseEvent e)
Sent when a traverse event occurs in a control.

Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static int shellWidth = 400;
 private static int shellHeight = 400;

 private static void addWidgetsToShell(Display display, Shell shell) {

  FocusListener focusListener = new FocusListener() {

   @Override
   public void focusGained(FocusEvent e) {
    System.out.println(e.widget + " got focus");
   }

   @Override
   public void focusLost(FocusEvent e) {
    System.out.println(e.widget + " lost focus");
   }

  };

  TraverseListener traverseListener = new TraverseListener() {

   @Override
   public void keyTraversed(TraverseEvent e) {
    System.out.println(e.widget + " was traversed");
   }

  };

  Button b1 = new Button(shell, SWT.PUSH);
  Button b2 = new Button(shell, SWT.PUSH);
  Button b3 = new Button(shell, SWT.PUSH);
  Button b4 = new Button(shell, SWT.PUSH);
  Button b5 = new Button(shell, SWT.PUSH);
  Button b6 = new Button(shell, SWT.PUSH);
  b1.setBounds(10, 10, 50, 50);
  b2.setBounds(100, 10, 50, 50);
  b3.setBounds(200, 10, 50, 50);
  b4.setBounds(10, 100, 50, 50);
  b5.setBounds(100, 100, 50, 50);
  b6.setBounds(200, 100, 50, 50);
  b1.setText("1");
  b2.setText("2");
  b3.setText("3");
  b4.setText("4");
  b5.setText("5");
  b6.setText("6");

  b1.addFocusListener(focusListener);
  b2.addFocusListener(focusListener);
  b3.addFocusListener(focusListener);
  b4.addFocusListener(focusListener);
  b5.addFocusListener(focusListener);
  b6.addFocusListener(focusListener);

  b1.addTraverseListener(traverseListener);
  b2.addTraverseListener(traverseListener);
  b3.addTraverseListener(traverseListener);
  b4.addTraverseListener(traverseListener);
  b5.addTraverseListener(traverseListener);
  b6.addTraverseListener(traverseListener);

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment