Java by API/org.eclipse.swt.widgets/Text

Материал из Java эксперт
Перейти к: навигация, поиск

new Text(Composite parent, int style)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A ColorDialog Example");
    s.setLayout(new FillLayout(SWT.VERTICAL));
    final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
    final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
    b.setText("Change Color");
    b.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        ColorDialog cd = new ColorDialog(s);
        cd.setText("ColorDialog Demo");
        cd.setRGB(new RGB(255, 255, 255));
        RGB newColor = cd.open();
        if (newColor == null) {
          return;
        }
        t.setBackground(new Color(d, newColor));
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Text(Shell shell, SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a single line text field
    new Text(shell, SWT.BORDER);
    // Create a right-aligned single line text field
    new Text(shell, SWT.RIGHT | SWT.BORDER);
    // Create a password text field
    new Text(shell, SWT.PASSWORD | SWT.BORDER);
    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
    // Create a multiple line text field
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Text(Shell shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a single line text field
    new Text(shell, SWT.BORDER);
    // Create a right-aligned single line text field
    new Text(shell, SWT.RIGHT | SWT.BORDER);
    // Create a password text field
    new Text(shell, SWT.PASSWORD | SWT.BORDER);
    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
    // Create a multiple line text field
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Text(Shell shell, SWT.PASSWORD | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a single line text field
    new Text(shell, SWT.BORDER);
    // Create a right-aligned single line text field
    new Text(shell, SWT.RIGHT | SWT.BORDER);
    // Create a password text field
    new Text(shell, SWT.PASSWORD | SWT.BORDER);
    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
    // Create a multiple line text field
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Text(Shell shell, SWT.READ_ONLY | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a single line text field
    new Text(shell, SWT.BORDER);
    // Create a right-aligned single line text field
    new Text(shell, SWT.RIGHT | SWT.BORDER);
    // Create a password text field
    new Text(shell, SWT.PASSWORD | SWT.BORDER);
    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
    // Create a multiple line text field
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Text(Shell shell, SWT.RIGHT | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a single line text field
    new Text(shell, SWT.BORDER);
    // Create a right-aligned single line text field
    new Text(shell, SWT.RIGHT | SWT.BORDER);
    // Create a password text field
    new Text(shell, SWT.PASSWORD | SWT.BORDER);
    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
    // Create a multiple line text field
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Text(Shell s, SWT.MULTI | SWT.BORDER)

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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Text Example");
    Text text1 = new Text(s, SWT.MULTI | SWT.BORDER);
    text1.setBounds(0,0,250,250);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Text(Shell s, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Composite Example");
    s.setLayout(new FillLayout());
    TextPaneComposite tpc = new TextPaneComposite(s);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}
class TextPaneComposite extends Composite {
  public TextPaneComposite(Composite parent) {
    super(parent, SWT.BORDER);
    this.setLayout(new FillLayout());
    Text t = new Text(this, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
  }
}





new Text(Shell s, SWT.SINGLE)

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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Text Example");
    final Text text1 = new Text(s, SWT.SINGLE);
    text1.setBounds(10,10,100,20);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Text(Shell s, SWT.SINGLE| SWT.BORDER)

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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Text Example");
    final Text text1 = new Text(s, SWT.SINGLE| SWT.BORDER);
    text1.setBounds(10,10,100,20);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Text(Shell s, SWT.WRAP | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: addFocusListener(FocusListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: addHelpListener(HelpListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    Shell shell = new Shell(display);
    Text fahrenheit = new Text(shell, SWT.BORDER);
    fahrenheit.setData("Type a temperature in Fahrenheit");
    fahrenheit.setBounds(20,20,100,20);
    
    fahrenheit.addHelpListener(new HelpListener(){
      public void helpRequested(HelpEvent event) {
        System.out.println((String) event.widget.getData());
      }
     
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Text: addKeyListener(KeyListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    
    final Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setSize(250, 200);
    
    
    shell.setLayout(new FillLayout());
    Text text = new Text(shell, SWT.BORDER);
    
    text.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent event) {
        switch (event.keyCode) {
        case SWT.CR:
          System.out.println(SWT.CR);
        case SWT.ESC:
          System.out.println(SWT.ESC);
          break;
        }
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: addModifyListener(ModifyListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    Shell shell = new Shell(display);
    Text fahrenheit = new Text(shell, SWT.BORDER);
    fahrenheit.setData("Type a temperature in Fahrenheit");
    fahrenheit.setBounds(20, 20, 100, 20);
    fahrenheit.addModifyListener(new ModifyListener(){
      public void modifyText(ModifyEvent event) {
        // Get the widget whose text was modified
        Text text = (Text) event.widget;
        System.out.println(text.getText());
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Text: addVerifyListener(VerifyListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    Shell shell = new Shell(display);
    Text fahrenheit = new Text(shell, SWT.BORDER);
    fahrenheit.setData("Type a temperature in Fahrenheit");
    fahrenheit.setBounds(20, 20, 100, 20);
    fahrenheit.addVerifyListener(new VerifyListener() {
      public void verifyText(VerifyEvent event) {
        // Assume we don"t allow it
        event.doit = false;
        // Get the character typed
        char myChar = event.character;
        String text = ((Text) event.widget).getText();
        // Allow "-" if first character
        if (myChar == "-" && text.length() == 0)
          event.doit = true;
        // Allow 0-9
        if (Character.isDigit(myChar))
          event.doit = true;
        // Allow backspace
        if (myChar == "\b")
          event.doit = true;
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Text: clearSelection()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: getSelectionCount()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: selectAll()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setData(Object data)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    Shell shell = new Shell(display);
    Text fahrenheit = new Text(shell, SWT.BORDER);
    fahrenheit.setData("Type a temperature in Fahrenheit");
    fahrenheit.setBounds(20,20,100,20);
    
    fahrenheit.addHelpListener(new HelpListener(){
      public void helpRequested(HelpEvent event) {
        System.out.println((String) event.widget.getData());
      }
     
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Text: setEchoChar(char ch)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    final Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setEchoChar("*");
    final Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t == text2) {
          if (t.getText().length() < 3) {
            t.setFocus();
          }
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setFocus()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    final Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setEchoChar("*");
    final Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t == text2) {
          if (t.getText().length() < 3) {
            t.setFocus();
          }
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setFont(Font f)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A ColorDialog Example");
    s.setLayout(new FillLayout(SWT.VERTICAL));
    final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
    final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
    b.setText("Change Color");
    b.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FontDialog fd = new FontDialog(s, SWT.NONE);
        fd.setText("Select Font");
        fd.setRGB(new RGB(0,0,255));
        FontData defaultFont = new FontData("Courier",10,SWT.BOLD);
        fd.setFontData(defaultFont);     
        FontData newFont = fd.open();
        if(newFont==null)
            return;
        t.setFont(new Font(d, newFont));
        t.setForeground(new Color(d, fd.getRGB()));
        
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setLayoutData(Object layoutData)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250,250);
    s.setText("A FormLayout Example");
    s.setLayout(new FormLayout());
    
    final Label l1 = new Label(s, SWT.RIGHT);
    l1.setText("First Name");
    FormData fd = new FormData();
    fd.top = new FormAttachment(10, 10);
    fd.left = new FormAttachment(0, 10);
    fd.bottom = new FormAttachment(30,0);
    fd.right = new FormAttachment(40,0);
    l1.setLayoutData(fd);
    
    final Label l2 = new Label(s, SWT.RIGHT);
    l2.setText("Last Name");
    fd = new FormData();
    fd.top = new FormAttachment(l1, 5);
    fd.left = new FormAttachment(0, 10);
    fd.bottom = new FormAttachment(40,0);
    fd.right = new FormAttachment(40,0);
    l2.setLayoutData(fd);
    
    final Text t1 = new Text(s, SWT.BORDER | SWT.SINGLE);
    fd = new FormData();
    fd.top = new FormAttachment(l1, 0, SWT.TOP);
    fd.left = new FormAttachment(l1, 10);
    t1.setLayoutData(fd);
    
    final Text t2 = new Text(s, SWT.BORDER | SWT.SINGLE);
    fd = new FormData();
    fd.top = new FormAttachment(l2, 0, SWT.TOP);
    fd.left = new FormAttachment(l2, 10);
    t2.setLayoutData(fd);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setTextLimit(int num)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(100, 50, 100, 20);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(100, 75, 100, 20);
    text2.setTextLimit(30);
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Text) e.widget;
        t.selectAll();
      }
      public void focusLost(FocusEvent e) {
        Text t = (Text) e.widget;
        if (t.getSelectionCount() > 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Text: setText(String value)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Slider Example");
    final Slider slide = new Slider(s, SWT.HORIZONTAL);
    slide.setBounds(15, 50, 125, 15);
    slide.setMinimum(0);
    slide.setMaximum(100);
    slide.setIncrement(1);
    final Text t = new Text(s, SWT.BORDER);
    t.setBounds(115, 25, 25, 25);
    t.setText("0");
    slide.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        t.setText(new Integer(slide.getSelection()).toString());
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}