Friday 25 November 2016

SWT: Button: Create a button

Button widget is used to create a button, and clicked by user to perform some processing.

Ex:
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Submit");
button1.setLocation(0, 0);
button1.setSize(100, 20);

Above statements create a button with text ‘Submit’. You can also set the style, background, image and more by using set methods.

You can apply following styles to Button element.
Style
Description
SWT.ARROW
Style constant for arrow button behavior
SWT.CHECK
Style constant for check box behavior
SWT.PUSH
Style constant for push button behavior
SWT.RADIO
Style constant for radio button behavior
SWT.TOGGLE
Style constant for toggle button behavior
SWT.FLAT
Style constant for flat appearance.
SWT.WRAP
Style constant for automatic line wrap behavior.
SWT.UP
Style constant for align up behavior
SWT.DOWN
Style constant for align down behavior
SWT.LEFT
Style constant for align left behavior
SWT.RIGHT
Style constant for align right behavior
SWT.CENTER
Style constant for align center behavior

Note
a.   Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE may be specified.
b.   Only one of the styles LEFT, RIGHT, and CENTER may be specified.
c.    Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified when the ARROW style is specified.

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Test {

  private static int xPosition = 30;
  private static int yPosition = 30;
  private static int width = 500;
  private static int height = 30;
  private static int scrollWidth = 500;
  private static int scrollHeight = 350;

  private static int shellWidth = 600;
  private static int shellHeight = 600;

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

    Text text1 = new Text(shell, SWT.BORDER);
    text1.setText("You can't edit me");
    text1.setBounds(xPosition, yPosition, width, height);
    text1.setEditable(false);

    yPosition += 40;

    Text text2 = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    text2.setText("Bordered scrolled text box");
    text2.setBounds(xPosition, yPosition, scrollWidth, scrollHeight);

    yPosition += 80;

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

    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Cancel");
    button2.setLocation(xPosition + 260, yPosition + 300);
    button2.setSize(100, 20);

  }

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

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



Previous                                                 Next                                                 Home

No comments:

Post a Comment