Friday 25 November 2016

SWT: Text: Make the text box read only

By using the method ‘setEditable’, you can make the text box non-editable.

Ex:
text1.setEditable(false);

Another way to accomplish this would be to give it the style SWT.READ_ONLY.
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("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);

  }

  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