View RSS Feed

My Java Tips

SWT Button

Rate this Entry
by , 11-10-2011 at 05:44 PM (2129 Views)
Button is a selectable user interface object that issues notification when pressed and released. It is a widget that is clicked by the user in order to initiate some processing.

The following are the important constructors of Button class.

Button(Composite parent, int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The following are the important methods of Button class.

void addSelectionListener(SelectionListener listener)
Adds the listener to the collection of listeners who will be notified when the control is selected, by sending it one of the messages defined in the SelectionListener interface.

Point computeSize(int wHint, int hHint, boolean changed)
Returns the preferred size of the receiver.

int getAlignment()
Returns a value which describes the position of the text or image in the receiver.

Image getImage()
Returns the receiver's image if it has one, or null if it does not.

boolean getSelection()
Returns true if the receiver is selected, and false otherwise.

String getText()
Returns the receiver's text, which will be an empty string if it has never been set or if the receiver is an ARROW button.

void removeSelectionListener(SelectionListener listener)
Removes the listener from the collection of listeners who will be notified when the control is selected.

void setAlignment(int alignment)
Controls how text, images and arrows will be displayed in the receiver.

void setImage(Image image)
Sets the receiver's image to the argument, which may be null indicating that no image should be displayed.

void setSelection(boolean selected)
Sets the selection state of the receiver, if it is of type CHECK, RADIO, or TOGGLE.

void setText(String string)
Sets the receiver's text.

The different button styles are ARROW, CHECK, PUSH, RADIO, and TOGGLE. When the ARROW style is specified, only one of the styles UP, DOWN, LEFT, and RIGHT may be specified. Please note only one of the styles LEFT, RIGHT, and CENTER may be specified.


The example below creates a textfield with Label `Enter your name` along with two buttons OK and Cancel. When you click on OK it shows OK on console and when you click on Cancel it shows Cancel on console.

Java Code:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

public class SWTButton {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		Label label = new Label(shell, SWT.NONE);
		label.setText("Enter your name:");
		Text text = new Text(shell, SWT.BORDER);
		text.setLayoutData(new RowData(100, SWT.DEFAULT));
		Button ok = new Button(shell, SWT.PUSH);
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
		Button cancel = new Button(shell, SWT.PUSH);
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
		shell.setLayout(new RowLayout());
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())

Submit "SWT Button" to Facebook Submit "SWT Button" to Digg Submit "SWT Button" to Submit "SWT Button" to StumbleUpon Submit "SWT Button" to Google

SWT / JFace