SWT Button Examples

  • 01 June 2017
  • ADM

 

SWT Button Examples - images/logos/eclipse.jpg

 

Here are some examples of SWT buttons.

SWT button can have the following styles:

Style Description
SWT.ARROW Creates a push button that displays an arrow.
SWT.CHECK Creates a checkbox.
SWT.PUSH Creates a push button.
SWT.RADIO Creates a radio button.
SWT.TOGGLE Creates a push button that preserves its pushed or non-pushed state.
SWT.FLAT Creates a push button that appears flat.
SWT.UP When combined with SWT.ARROW, displays an upward-pointing arrow.
SWT.DOWN When combined with SWT.ARROW, displays a downward-pointing arrow.
SWT.LEFT Left-aligns the associated text. When combined with SWT.ARROW, displays a leftward-pointing arrow.
SWT.RIGHT Right-aligns the associated text. When combined with SWT.ARROW, displays a rightward-pointing arrow.

Example

package com.admfactory.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ButtonExampleSWT {
    public static void main(String[] args) {
	Display display = new Display();
	/** create the new window */
	Shell shell = new Shell(display);

	/** adding the window title */
	shell.setText("Buttons examples");

	/** add a layout of 4 columns */
	shell.setLayout(new GridLayout(4, true));

	/** Create four push buttons */
	new Button(shell, SWT.PUSH).setText("Push 1");
	new Button(shell, SWT.PUSH).setText("Push 2");
	new Button(shell, SWT.PUSH).setText("Push 3");
	new Button(shell, SWT.PUSH).setText("Push 4");

	/** Create four check buttons */
	new Button(shell, SWT.CHECK).setText("Checkbox 1");
	new Button(shell, SWT.CHECK).setText("Checkbox 2");
	new Button(shell, SWT.CHECK).setText("Checkbox 3");
	new Button(shell, SWT.CHECK).setText("Checkbox 4");

	/** Create four toggle buttons */
	new Button(shell, SWT.TOGGLE).setText("Toggle 1");
	new Button(shell, SWT.TOGGLE).setText("Toggle 2");
	new Button(shell, SWT.TOGGLE).setText("Toggle 3");
	new Button(shell, SWT.TOGGLE).setText("Toggle 4");

	/** Create four radio buttons */
	new Button(shell, SWT.RADIO).setText("Radio 1");
	new Button(shell, SWT.RADIO).setText("Radio 2");
	new Button(shell, SWT.RADIO).setText("Radio 3");
	new Button(shell, SWT.RADIO).setText("Radio 4");

	/** Create four flat buttons */
	new Button(shell, SWT.FLAT).setText("Flat 1");
	new Button(shell, SWT.FLAT).setText("Flat 2");
	new Button(shell, SWT.FLAT).setText("Flat 3");
	new Button(shell, SWT.FLAT).setText("Flat 4");

	/** Create four arrows buttons */
	new Button(shell, SWT.ARROW | SWT.LEFT);
	new Button(shell, SWT.ARROW | SWT.UP);
	new Button(shell, SWT.ARROW | SWT.DOWN);
	new Button(shell, SWT.ARROW | SWT.RIGHT);

	/** Resize the shell to minimum size determined by the components */
	shell.pack();

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

	/** Loop to keep the application opened */
	while (!shell.isDisposed()) {
	    if (!display.readAndDispatch()) {
		display.sleep();
	    }
	}
	display.dispose();
    }
}

Output

SWT Button Examples - /images/SWTButtons01.png

 

References