Default Button : Button « SWT JFace Eclipse « Java






Default Button

Default Button

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 25, 2004 11:46:01 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.com/swt
 *****************************************************************************/



import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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 SWTDefaultButton {

  public SWTDefaultButton() {
    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setLayout(new RowLayout());

    final String[] ratings =
      new String[] { "Killer!", "Good stuff", "So-so", "Needs work" };
    final Button[] radios = new Button[ratings.length];
    for (int i = 0; i < ratings.length; i++) {
      radios[i] = new Button(shell, SWT.RADIO);
      radios[i].setText(ratings[i]);
    }

    Button cancelButton = new Button(shell, SWT.PUSH);
    cancelButton.setText("Canel");

    Button rateButton = new Button(shell, SWT.PUSH);
    rateButton.setText("Rate!");
    rateButton.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        for (int i = 0; i < radios.length; i++)
          if (radios[i].getSelection())
            System.out.println("Rating: " + ratings[i]);

      }

      public void widgetDefaultSelected(SelectionEvent e) {
        System.out.println("Default selection");
      }
    });

    shell.setDefaultButton(rateButton);
    System.out.println(shell.getDefaultButton());

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }

  public static void main(String[] args) {
    new SWTDefaultButton();
  }
}


           
       








Related examples in the same category

1.SWT ButtonSWT Button
2.SWT Button Example DemoSWT Button Example Demo
3.SWT Button ActionSWT Button Action
4.Icon SelectorIcon Selector
5.Button StylesButton Styles
6.Image Button
7.Widget StylesWidget Styles
8.Demonstrates ButtonsDemonstrates Buttons
9.Button ExampleButton Example
10.Arrow Button ExampleArrow Button Example
11.Button Selection Event
12.Make a toggle button have radio behaviorMake a toggle button have radio behavior
13.Set the default buttonSet the default button