Tuesday 22 November 2016

SWT: text widget

Text widget is used to create text boxes that allow the user to enter and modify text.

You can apply following styles to Text widget.

Style
Description
CENTER
Style constant for align center behavior
ICON_CANCEL
The style constant for "cancel" icon. This style constant is used with Text in combination with SWT.SEARCH
ICON_SEARCH
The style constant for "cancel" icon. This style constant is used with Text in combination with SWT.SEARCH
LEFT
Style constant for align left behavior
MULTI
Style constant for multi-selection behavior in lists and multiple line support on text fields
PASSWORD
Style constant for password behavior.
SEARCH
Style constant for search behavior
SINGLE
Style constant for single selection behavior in lists and single line support on text fields
RIGHT
Style constant for align right behavior
READ_ONLY
Style constant for read-only behavior
WRAP
Style constant for automatic line wrap behavior


Only one of the styles MULTI and SINGLE may be specified, and only one of the styles LEFT, CENTER, and RIGHT may be specified.
package swt_app;

import org.eclipse.swt.SWT;
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("Bordered text box");
  text1.setBounds(xPosition, yPosition, width, height);
  text1.setTextLimit(30);

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

 }

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


Run above application, you can able to see following screen.





Previous                                                 Next                                                 Home

1 comment:

  1. private static void addTextToShell(Display display, Shell shell) is this a method or package or class

    ReplyDelete