Saturday 3 December 2016

SWT: SelectionListener

SelectionListener provides methods to handle the events, when the widget is selected like button clicks. SelectionListener interface provides following methods.

Method
Description
public void widgetSelected(SelectionEvent e)
Method is called, when selectio event occurred on given SWR widget. For example, button clicks, item selects in list etc.,
public void widgetDefaultSelected(SelectionEvent e)
Sent when default selection occurs in the control.


Example
button1.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
                 int countryIndex = countryCombo.getSelectionIndex();
                 int hobbyIndex = hobbyCombo.getSelectionIndex();

                 String country = "No Country Selected";
                 String hobby = "No Hobby Selected";

                 country = (countryIndex > -1) ? countries[countryIndex] : country;
                 hobby = (hobbyIndex > -1) ? hobbies[hobbyIndex] : hobby;

                 System.out.println("Country selected : " + country);
                 System.out.println("Hobby selected : " + hobby);

         }
});

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static int xPosition = 30;
 private static int yPosition = 50;

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

 private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" };
 private static String[] hobbies = { "Trekking", "Movies", "Games", "Painting", "Singing", "Dancing" };

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

  Combo countryCombo = new Combo(shell, SWT.DROP_DOWN);
  countryCombo.setItems(countries);
  countryCombo.setBounds(xPosition, yPosition, 150, 100);
  countryCombo.setText("Select Country");

  yPosition += 50;

  Combo hobbyCombo = new Combo(shell, SWT.DROP_DOWN);
  hobbyCombo.setItems(hobbies);
  hobbyCombo.setBounds(xPosition, yPosition, 150, 100);
  hobbyCombo.setText("Select hobbies");

  yPosition += 50;

  Button button1 = new Button(shell, SWT.PUSH);
  button1.setText("Submit");
  button1.setLocation(xPosition + 100, yPosition);
  button1.setSize(100, 20);

  button1.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    int countryIndex = countryCombo.getSelectionIndex();
    int hobbyIndex = hobbyCombo.getSelectionIndex();

    String country = "No Country Selected";
    String hobby = "No Hobby Selected";

    country = (countryIndex > -1) ? countries[countryIndex] : country;
    hobby = (hobbyIndex > -1) ? hobbies[hobbyIndex] : hobby;

    System.out.println("Country selected : " + country);
    System.out.println("Hobby selected : " + hobby);

   }
  });

 }

 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();
 }
}



SelectionAdapter is an adpater class that implements SelectionListener interface. Adapter classes provide default (often empty) implementation of interface or abstract class. With the use of adapter class, you can provide only the implementation of methods you needed.


Previous                                                 Next                                                 Home

No comments:

Post a Comment