org.eclipse.swt.widgets
Class Button

java.lang.Object
  |
  +--org.eclipse.swt.widgets.Widget
        |
        +--org.eclipse.swt.widgets.Control
              |
              +--org.eclipse.swt.widgets.Button
All Implemented Interfaces:
Drawable, Runnable

public class Button
extends Control

This class provides an implementation for the Button GUI element.

The following code fragment creates a Button inside a Shell, sets its text, and associates a Listener for the event that the user presses the button:

        Display display=new Display();
        Shell shell=new Shell(display);
        
        Button close=new Button(shell, SWT.PUSH);
        close.setText("CLOSE");

        close.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event e) {
                    System.out.println("CLOSE pressed.");
                }
        });
        
        shell.setLayout(new RowLayout());
        shell.pack();

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


Field Summary
 
Fields inherited from class org.eclipse.swt.widgets.Widget
h, INCMD_DISPOSE, INCMD_EXTKEY, INCMD_KEY, INCMD_LDOWN, INCMD_LUP, INCMD_MENU, INCMD_POS, UPDATEITEM_ENABLED, UPDATEITEM_NAME, w, x, y
 
Constructor Summary
Button(Composite composite, int s)
          Constructs an instance of this class.
 
Method Summary
 void setSelection(boolean selected)
          Sets the selection state of the Button.
 void setText(String string)
          This method sets label of the Widget.
 
Methods inherited from class org.eclipse.swt.widgets.Control
computeSize, computeSize, getLayoutData, isFocusControl, pack, pack, setBackground, setBounds, setFocus, setLayoutData, setSize, setSize, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addListener, dispose, focusState, getBorderWidth, getDisplayArea, getSelection, getStyle, getText, isDisposed, removeListener, setBounds, setCursor, stop
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, toString
 
Methods inherited from interface org.eclipse.swt.graphics.Drawable
drawLine, drawRect, drawText, setColor
 
Methods inherited from interface java.lang.Runnable
run
 

Constructor Detail

Button

public Button(Composite composite,
              int s)
Constructs an instance of this class. Parameters are the parent component, and a style value describing behavior and appearance. The style value is a combination of the constants defined in the class SWT.

Parameters:
composite - - a composite control being the parent of the new instance (cannot be null)
s - - the style of the control to be constructed
Method Detail

setSelection

public void setSelection(boolean selected)
Sets the selection state of the Button. This method has an effect only if the Button is of type CHECK, RADIO, or TOGGLE.

When the receiver is of type CHECK or RADIO, it is selected when it is checked. When it is of type TOGGLE, it is selected when it is pushed in.

Parameters:
selected - the new selection state
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

setText

public void setText(String string)
This method sets label of the Widget. The label text may include the mnemonic character and line delimiters. Mnemonics are indicated by the character '&', causing the next character to be the mnemonic. When the user presses a key sequence that matches the mnemonic, the focus is assigned to the control that follows the label. On most platforms, the mnemonic appears underlined but may be emphasised in a platform specific manner. The mnemonic indicator character '&' can be escaped by doubling it in the string, causing a single '&' to be displayed.

Parameters:
string - - the widget label