Java/SWT JFace Eclipse/Text
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Add a select all menu item to the control
- 2 Demonstrates multiline comments
- 3 Demonstrates text fields
- 4 Detect CR in a text or combo control (default selection)
- 5 Detect when the user scrolls a text control
- 6 Draw internationalized styled text on a shell
- 7 Prompt for a password (set the echo character)
- 8 Remarks Text
- 9 Resize a text control (show about 10 characters)
- 10 Select all the text in the control
- 11 Set the selection (start, end)
- 12 Stop CR from going to the default button
- 13 SWT XML Editor: Modify DOM
- 14 Text and Label demo
- 15 Text Event
- 16 Text example snippet: set the selection (i-beam)
- 17 TextField Example
- 18 TextField Example 2
- 19 TextField Example 3
- 20 TextField Example 4
- 21 TextField Example 5
- 22 Text to uppercase
- 23 Turns e characters red using a LineStyleListener
- 24 Verify input (format for date)
- 25 Verify input (only allow digits)
- 26 Wrap Lines
/*
* Text example snippet: add a select all menu item to the control
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet117 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Text t = new Text(shell, SWT.BORDER | SWT.MULTI);
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
MenuItem editItem = new MenuItem(bar, SWT.CASCADE);
editItem.setText("Edit");
Menu submenu = new Menu(shell, SWT.DROP_DOWN);
editItem.setMenu(submenu);
MenuItem item = new MenuItem(submenu, SWT.PUSH);
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
t.selectAll();
}
});
item.setText("Select &All\tCtrl+A");
item.setAccelerator(SWT.CTRL + "A");
// Note that as long as your application has not overridden
// the global accelerators for copy, paste, and cut
// (CTRL+C or CTRL+INSERT, CTRL+V or SHIFT+INSERT, and CTRL+X or
// SHIFT+DELETE)
// these behaviours are already available by default.
// If your application overrides these accelerators,
// you will need to call Text.copy(), Text.paste() and Text.cut()
// from the Selection callback for the accelerator when the
// text widget has focus.
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Demonstrates multiline comments
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import java.util.LinkedList;
import java.util.ArrayList;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
/**
* This program demonstrates multiline comments. It uses MultiLineCommentListener
* to do the syntax coloring
*/
public class MultiLineComment {
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Multiline Comments");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FillLayout());
final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
// Add the line style listener
final MultiLineCommentListener lineStyleListener = new MultiLineCommentListener();
styledText.addLineStyleListener(lineStyleListener);
// Add the modification listener
styledText.addExtendedModifyListener(new ExtendedModifyListener() {
public void modifyText(ExtendedModifyEvent event) {
// Recalculate the comments
lineStyleListener.refreshMultilineComments(styledText.getText());
// Redraw the text
styledText.redraw();
}
});
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new MultiLineComment().run();
}
}
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
/**
* This class supports multiline comments. It turns comments green.
*/
class MultiLineCommentListener implements LineStyleListener {
// Markers for multiline comments
private static final String COMMENT_START = "/*";
private static final String COMMENT_END = "*/";
// Color for comments
private static final Color COMMENT_COLOR = Display.getCurrent().getSystemColor(
SWT.COLOR_DARK_GREEN);
// Offsets for all multiline comments
List commentOffsets;
/**
* MultilineCommentListener constructor
*/
public MultiLineCommentListener() {
commentOffsets = new LinkedList();
}
/**
* Refreshes the offsets for all multiline comments in the parent StyledText.
* The parent StyledText should call this whenever its text is modified. Note
* that this code doesn"t ignore comment markers inside strings.
*
* @param text the text from the StyledText
*/
public void refreshMultilineComments(String text) {
// Clear any stored offsets
commentOffsets.clear();
// Go through all the instances of COMMENT_START
for (int pos = text.indexOf(COMMENT_START); pos > -1; pos = text.indexOf(
COMMENT_START, pos)) {
// offsets[0] holds the COMMENT_START offset
// and COMMENT_END holds the ending offset
int[] offsets = new int[2];
offsets[0] = pos;
// Find the corresponding end comment.
pos = text.indexOf(COMMENT_END, pos);
// If no corresponding end comment, use the end of the text
offsets[1] = pos == -1 ? text.length() - 1 : pos + COMMENT_END.length() - 1;
pos = offsets[1];
// Add the offsets to the collection
commentOffsets.add(offsets);
}
}
/**
* Called by StyledText to get the styles for a line
*
* @param event the event
*/
public void lineGetStyle(LineStyleEvent event) {
// Create a collection to hold the StyleRanges
List styles = new ArrayList();
// Store the length for convenience
int length = event.lineText.length();
for (int i = 0, n = commentOffsets.size(); i < n; i++) {
int[] offsets = (int[]) commentOffsets.get(i);
// If starting offset is past current line--quit
if (offsets[0] > event.lineOffset + length) break;
// Check if we"re inside a multiline comment
if (offsets[0] <= event.lineOffset + length
&& offsets[1] >= event.lineOffset) {
// Calculate starting offset for StyleRange
int start = Math.max(offsets[0], event.lineOffset);
// Calculate length for style range
int len = Math.min(offsets[1], event.lineOffset + length) - start + 1;
// Add the style range
styles.add(new StyleRange(start, len, COMMENT_COLOR, null));
}
}
// Copy all the ranges into the event
event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
}
}
Demonstrates text fields
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates text fields
*/
public class TextExample {
public static void main(String[] args) {
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();
}
}
Detect CR in a text or combo control (default selection)
/*
* example snippet: detect CR in a text or combo control (default selelection)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet24 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Combo combo = new Combo(shell, SWT.NONE);
combo.setItems(new String[] { "A-1", "B-1", "C-1" });
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setText("some text");
combo.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
System.out.println(e.widget + " - Default Selection");
}
});
text.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
System.out.println(e.widget + " - Default Selection");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Detect when the user scrolls a text control
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/*
* Detect when the user scrolls a text control
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
public class Snippet191 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
for (int i = 0; i < 32; i++) {
text.append(i + "-This is a line of text in a widget-" + i + "\n");
}
text.setSelection(0);
Listener listener = new Listener() {
int lastIndex = text.getTopIndex();
public void handleEvent(Event e) {
int index = text.getTopIndex();
if (index != lastIndex) {
lastIndex = index;
System.out.println("Scrolled, topIndex=" + index);
}
}
};
/* NOTE: Only detects scrolling by the user */
text.addListener(SWT.MouseDown, listener);
text.addListener(SWT.MouseMove, listener);
text.addListener(SWT.MouseUp, listener);
text.addListener(SWT.KeyDown, listener);
text.addListener(SWT.KeyUp, listener);
text.addListener(SWT.Resize, listener);
ScrollBar hBar = text.getHorizontalBar();
if (hBar != null) {
hBar.addListener(SWT.Selection, listener);
}
ScrollBar vBar = text.getVerticalBar();
if (vBar != null) {
vBar.addListener(SWT.Selection, listener);
}
shell.pack();
Point size = shell.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
shell.setSize(size.x - 32, size.y / 2);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Draw internationalized styled text on a shell
/*
* TextLayout example snippet: draw internationalized styled text on a shell
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Snippet145 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD);
Font font2 = new Font(display, "MS Mincho", 20, SWT.ITALIC);
Font font3 = new Font(display, "Arabic Transparent", 18, SWT.NORMAL);
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Color green = display.getSystemColor(SWT.COLOR_GREEN);
Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
Color gray = display.getSystemColor(SWT.COLOR_GRAY);
final TextLayout layout = new TextLayout(display);
TextStyle style1 = new TextStyle(font1, yellow, blue);
TextStyle style2 = new TextStyle(font2, green, null);
TextStyle style3 = new TextStyle(font3, blue, gray);
layout
.setText("English \u65E5\u672C\u8A9E \u0627\u0644\u0639\u0631\u0628\u064A\u0651\u0629");
layout.setStyle(style1, 0, 6);
layout.setStyle(style2, 8, 10);
layout.setStyle(style3, 13, 21);
shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
layout.draw(event.gc, 10, 10);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
font1.dispose();
font2.dispose();
font3.dispose();
layout.dispose();
display.dispose();
}
}
Prompt for a password (set the echo character)
/*
* Text example snippet: prompt for a password (set the echo character)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet121 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setEchoChar("*");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Remarks Text
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 17, 2004 9:58:26 PM by JACK
* $Id$
*
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 RemarksText {
Display display = new Display();
Shell shell = new Shell(display);
Text text;
public RemarksText() {
shell.setLayout(new GridLayout(1, false));
(new Label(shell, SWT.NULL)).setText("Remarks:");
text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
text.setText("123456789");
text.setLayoutData(new GridData(GridData.FILL_BOTH));
System.out.println("getText: " + text.getText(1, 6));
char[] chars = text.getLineDelimiter().toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println("Line delimiter #" + i + ": " + Integer.toHexString(chars[i]) );
}
text.getOrientation();
System.out.println("Number of chars: " + text.getCharCount());
System.out.println("Tabs: " + text.getTabs());
text.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
if(e.end == e.start) {
if( e.character == " " && (e.stateMask & SWT.CTRL) != 0 ) {
if(text.getText(e.end-1, e.end-1).equals("V")) {
e.text = "erifyListener";
}else{
e.doit = false;
}
}
}
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
System.out.println("New character count: " + text.getCharCount());
}
});
// text.append("a");
text.setSelection(1, 4);
System.out.println("getSelection:\t" + text.getSelection());
System.out.println("getSelectionCount:\t" + text.getSelectionCount());
System.out.println("getSelectionText:\t" + text.getSelectionText());
//text.insert("ABC");
init();
// shell.pack();
shell.setSize(300, 150);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new RemarksText();
}
}
Resize a text control (show about 10 characters)
/*
* Text example snippet: resize a text control (show about 10 characters)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet55 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.BORDER);
int columns = 10;
GC gc = new GC(text);
FontMetrics fm = gc.getFontMetrics();
int width = columns * fm.getAverageCharWidth();
int height = fm.getHeight();
gc.dispose();
text.setSize(text.ruputeSize(width, height));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Select all the text in the control
/*
* Text example snippet: select all the text in the control
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet22 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, 0);
text.setText("ASDF");
text.setSize(64, 32);
text.selectAll();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Set the selection (start, end)
/*
* Text example snippet: set the selection (start, end)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet12 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.BORDER | SWT.V_SCROLL);
text.setBounds(10, 10, 100, 100);
for (int i = 0; i < 16; i++) {
text.append("Line " + i + "\n");
}
shell.open();
text.setSelection(30, 38);
System.out.println(text.getCaretLocation());
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Stop CR from going to the default button
/*
* Text example snippet: stop CR from going to the default button
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 Snippet116 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setText("Here is some text");
text.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
System.out
.println("Text default selected (overrides default button)");
}
});
text.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
}
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Ok");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Button selected");
}
});
shell.setDefaultButton(button);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT XML Editor: Modify DOM
/*
* Browser example snippet: modify DOM (executing javascript)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ModifyDOM {
public static void main(String[] args) {
final String html = "<html><title>Snippet</title><body><p id="myid">Best Friends</p><p id="myid2">Cat and Dog</p></body></html>";
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.BORDER);
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new FillLayout(SWT.VERTICAL));
final Text text = new Text(comp, SWT.MULTI);
text
.setText("var newNode = document.createElement("P"); \r\n"
+ "var text = document.createTextNode("At least when I am around");\r\n"
+ "newNode.appendChild(text);\r\n"
+ "document.getElementById("myid").appendChild(newNode);\r\n"
+ "\r\n" + "document.bgColor="yellow";");
final Button button = new Button(comp, SWT.PUSH);
button.setText("Execute Script");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
boolean result = browser.execute(text.getText());
if (!result) {
/*
* Script may fail or may not be supported on certain
* platforms.
*/
System.out.println("Script was not executed.");
}
}
});
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Text and Label demo
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 29, 2004 3:09:11 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.rubo;
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 Sample {
Display display = new Display();
Shell shell = new Shell(display);
public Sample() {
shell.setText("Book Entry Demo");
GridLayout gridLayout = new GridLayout(4, false);
gridLayout.verticalSpacing = 8;
shell.setLayout(gridLayout);
// Title
Label label = new Label(shell, SWT.NULL);
label.setText("Title: ");
Text title = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
title.setLayoutData(gridData);
// Author(s)
label = new Label(shell, SWT.NULL);
label.setText("Author(s): ");
Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
authors.setLayoutData(gridData);
// Cover
label = new Label(shell, SWT.NULL);
label.setText("Cover: ");
gridData = new GridData();
gridData.verticalSpan = 3;
label.setLayoutData(gridData);
CLabel cover = new CLabel(shell, SWT.NULL);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 1;
gridData.verticalSpan = 3;
gridData.heightHint = 100;
gridData.widthHint = 100;
cover.setLayoutData(gridData);
// Details.
label = new Label(shell, SWT.NULL);
label.setText("Pages");
Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER);
pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Publisher");
Text pubisher = new Text(shell, SWT.SINGLE | SWT.BORDER);
pubisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Rating");
Combo rating = new Combo(shell, SWT.READ_ONLY);
rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
rating.add("5");
rating.add("4");
rating.add("3");
rating.add("2");
rating.add("1");
// Abstract.
label = new Label(shell, SWT.NULL);
label.setText("Abstract:");
Text bookAbstract =
new Text(
shell,
SWT.WRAP
| SWT.MULTI
| SWT.BORDER
| SWT.H_SCROLL
| SWT.V_SCROLL);
gridData =
new GridData(
GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
gridData.grabExcessVerticalSpace = true;
bookAbstract.setLayoutData(gridData);
// Button.
Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = GridData.END;
enter.setLayoutData(gridData);
// Fill information.
title.setText("Professional Java Interfaces with SWT/JFace");
authors.setText("Jack Li Guojie");
pages.setText("500pp");
pubisher.setText("John Wiley & Sons");
cover.setBackground(new Image(display, "jexp.gif"));
bookAbstract.setText(
"This book provides a comprehensive guide for \n"
+ "you to create Java user interfaces with SWT/JFace. ");
shell.pack();
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new Sample();
}
}
Text Event
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 16, 2004 1:23:25 AM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 TemperatureConverter {
Display display = new Display();
Shell shell = new Shell(display);
Label fahrenheitLabel;
Label celsiusLabel;
Label messageLabel;
Text fahrenheitValue;
Text celsiusValue;
public TemperatureConverter() {
shell.setText("SWT Temperature Converter");
shell.setLayout(new GridLayout(4, false));
fahrenheitLabel = new Label(shell, SWT.NULL);
fahrenheitLabel.setText("Fahrenheit: ");
fahrenheitValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
celsiusLabel = new Label(shell, SWT.NULL);
celsiusLabel.setText("Celsius: ");
celsiusValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
messageLabel = new Label(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 4;
messageLabel.setLayoutData(gridData);
ModifyListener listener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
valueChanged((Text) e.widget);
}
};
fahrenheitValue.addModifyListener(listener);
celsiusValue.addModifyListener(listener);
shell.pack();
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
/**
* Performs conversion when one of the text fields changes.
*
* @param text
* the event source
*/
public void valueChanged(Text text) {
if (!text.isFocusControl())
return;
if (text == fahrenheitValue) {
try {
double fValue = Double.parseDouble(text.getText());
double cValue = (fValue - 32) / 1.8;
celsiusValue.setText(Double.toString(cValue));
System.out.println("F -> C: " + cValue);
messageLabel.setText("Conversion performed successfully.");
} catch (NumberFormatException e) {
celsiusValue.setText("");
messageLabel.setText("Invalid number format: " + text.getText());
}
} else {
try {
double cValue = Double.parseDouble(text.getText());
double fValue = cValue * 1.8 + 32;
fahrenheitValue.setText(Double.toString(fValue));
System.out.println("C -> F: " + fValue);
messageLabel.setText("Conversion performed successfully.");
} catch (NumberFormatException e) {
fahrenheitValue.setText("");
messageLabel.setText("Invalid number format: " + text.getText());
}
}
}
public static void main(String[] args) {
new TemperatureConverter();
}
}
Text example snippet: set the selection (i-beam)
/*
* Text example snippet: set the selection (i-beam)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet11 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.BORDER | SWT.V_SCROLL);
text.setBounds(10, 10, 100, 100);
for (int i = 0; i < 16; i++) {
text.append("Line " + i + "\n");
}
shell.open();
text.setSelection(30);
System.out.println(text.getCaretLocation());
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
TextField Example
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 TextFieldExample {
Display d;
Shell s;
TextFieldExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
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();
}
public static void main(String[] arg) {
new TextFieldExample();
}
}
TextField Example 2
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 TextFieldExample2 {
Display d;
Shell s;
TextFieldExample2() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Text Field Example");
final Text text1 = new Text(s, SWT.SINGLE | SWT.BORDER);
text1.setBounds(100, 50, 100, 20);
final Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
text2.setBounds(100, 75, 100, 20);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] arg) {
new TextFieldExample2();
}
}
TextField Example 3
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 TextFieldExample3 {
Display d;
Shell s;
TextFieldExample3() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Text Field 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();
}
public static void main(String[] arg) {
new TextFieldExample3();
}
}
TextField Example 4
/**
The following code is from Michael Schmidt(MichaelMSchmidt (at) msn.ru).
The code is published under BSD license.
Thanks for the input from Michael Schmidt.
*/
package TextTest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* This is a revision of TextFieldExample4. Two modifications have been made.
* First, a Text value (ABC) has been added to the text2 field, simply for
* demonstration purposes. Second, a mouseDown() listener has been added to
* text2. Note that in the original sample, text2 would not highlight (perform
* the selectAll() operation) when it gained focus with the mouse. This is
* because clicking on the field invokes gainFocus() followed by mouseDown(),
* and mouseDown() does a clearSelection(), negating the selectAll() done in
* the gainFocus() method. You can test this by simply commenting out the
* addMouseListener code.
* <p>
* Other modifications are possible. The Text field may be designated
* read-only to prevent editing and erasing of the field when the tab key is
* used to move through the widgets. Logic can also be used to do a selectAll()
* when you first click on the field and then place the I-beam cursor if the
* mouse is clicked again.
*
* @author Michael Schmidt
*
*/
public class TextFieldExample4Revised {
Display d;
Shell s;
TextFieldExample4Revised() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Text Field Example");
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);
text2.setText("ABC");
// add a focus listener
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);
// Added code - comment out to test without the mouseListener
text2.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
Text t = (Text) e.widget;
t.selectAll();
}
});
// End of added code
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] arg) {
new TextFieldExample4Revised();
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TextFieldExample4 {
Display d;
Shell s;
TextFieldExample4() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Text Field Example");
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);
// add a focus listener
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();
}
public static void main(String[] arg) {
new TextFieldExample4();
}
}
TextField Example 5
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 TextFieldExample5 {
Display d;
Shell s;
TextFieldExample5() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A Text Field Example");
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);
// add a focus listener
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();
}
public static void main(String[] arg) {
new TextFieldExample5();
}
}
Text to uppercase
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Text;
public class Ch5Capitalizer extends Composite {
public Ch5Capitalizer(Composite parent) {
super(parent, SWT.NONE);
buildControls();
}
private void buildControls() {
this.setLayout(new FillLayout());
Text text = new Text(this, SWT.MULTI | SWT.V_SCROLL);
text.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
if (e.text.startsWith("1")) {
e.doit = false;
} else {
e.text = e.text.toUpperCase();
}
}
});
}
}
Turns e characters red using a LineStyleListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class turns "e" characters red using a LineStyleListener
*/
public class RedEListener {
// Color for the StyleRanges
private Color red;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
// Get color for style ranges
red = display.getSystemColor(SWT.COLOR_RED);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FillLayout());
// Create the StyledText
final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
// Add the syntax coloring handler
styledText.addLineStyleListener(new LineStyleListener() {
public void lineGetStyle(LineStyleEvent event) {
// Create a collection to hold the StyleRanges
java.util.List styles = new java.util.ArrayList();
// Iterate through the text
for (int i = 0, n = event.lineText.length(); i < n; i++) {
// Check for "e"
if (event.lineText.charAt(i) == "e") {
// Found an "e"; combine all subsequent e"s into the same StyleRange
int start = i;
for (; i < n && event.lineText.charAt(i) == "e"; i++);
// Create the StyleRange and add it to the collection
styles.add(new StyleRange(event.lineOffset + start, i - start, red,
null));
}
}
// Set the styles for the line
event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
}
});
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new RedEListener().run();
}
}
Verify input (format for date)
/*
* Text example snippet: verify input (format for date)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import java.util.Calendar;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet179 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Text text = new Text(shell, SWT.BORDER);
text.setText("YYYY/MM/DD");
;
final Calendar calendar = Calendar.getInstance();
text.addListener(SWT.Verify, new Listener() {
boolean ignore;
public void handleEvent(Event e) {
if (ignore)
return;
e.doit = false;
StringBuffer buffer = new StringBuffer(e.text);
char[] chars = new char[buffer.length()];
buffer.getChars(0, chars.length, chars, 0);
if (e.character == "\b") {
for (int i = e.start; i < e.end; i++) {
switch (i) {
case 0: /* [Y]YYY */
case 1: /* Y[Y]YY */
case 2: /* YY[Y]Y */
case 3: /* YYY[Y] */{
buffer.append("Y");
break;
}
case 5: /* [M]M */
case 6: /* M[M] */{
buffer.append("M");
break;
}
case 8: /* [D]D */
case 9: /* D[D] */{
buffer.append("D");
break;
}
case 4: /* YYYY[/]MM */
case 7: /* MM[/]DD */{
buffer.append("/");
break;
}
default:
return;
}
}
text.setSelection(e.start, e.start + buffer.length());
ignore = true;
text.insert(buffer.toString());
ignore = false;
text.setSelection(e.start, e.start);
return;
}
int start = e.start;
if (start > 9)
return;
int index = 0;
for (int i = 0; i < chars.length; i++) {
if (start + index == 4 || start + index == 7) {
if (chars[i] == "/") {
index++;
continue;
}
buffer.insert(index++, "/");
}
if (chars[i] < "0" || "9" < chars[i])
return;
if (start + index == 5 && "1" < chars[i])
return; /* [M]M */
if (start + index == 8 && "3" < chars[i])
return; /* [D]D */
index++;
}
String newText = buffer.toString();
int length = newText.length();
StringBuffer date = new StringBuffer(text.getText());
date.replace(e.start, e.start + length, newText);
calendar.set(Calendar.YEAR, 1901);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
String yyyy = date.substring(0, 4);
if (yyyy.indexOf("Y") == -1) {
int year = Integer.parseInt(yyyy);
calendar.set(Calendar.YEAR, year);
}
String mm = date.substring(5, 7);
if (mm.indexOf("M") == -1) {
int month = Integer.parseInt(mm) - 1;
int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
if (0 > month || month > maxMonth)
return;
calendar.set(Calendar.MONTH, month);
}
String dd = date.substring(8, 10);
if (dd.indexOf("D") == -1) {
int day = Integer.parseInt(dd);
int maxDay = calendar.getActualMaximum(Calendar.DATE);
if (1 > day || day > maxDay)
return;
calendar.set(Calendar.DATE, day);
} else {
if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) {
char firstChar = date.charAt(8);
if (firstChar != "D" && "2" < firstChar)
return;
}
}
text.setSelection(e.start, e.start + length);
ignore = true;
text.insert(newText);
ignore = false;
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Verify input (only allow digits)
/*
* Text example snippet: verify input (only allow digits)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet19 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.BORDER | SWT.V_SCROLL);
text.setBounds(10, 10, 200, 200);
text.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
String string = e.text;
char[] chars = new char[string.length()];
string.getChars(0, chars.length, chars, 0);
for (int i = 0; i < chars.length; i++) {
if (!("0" <= chars[i] && chars[i] <= "9")) {
e.doit = false;
return;
}
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Wrap Lines
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 17, 2004 8:02:20 PM by JACK
* $Id$
*
*****************************************************************************/
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class WrapLines {
Display display = new Display();
Shell shell = new Shell(display);
Text text1;
Text text2;
String line = "abcdefghijklmnopqrstuvwxyz0123456789";
private void init() {
text1 = new Text(shell, SWT.BORDER | SWT.MULTI);
//text.setTextLimit(12);
text1.setText(line);
text2 = new Text(shell, SWT.BORDER | SWT.WRAP);
text2.setText(line);
}
public WrapLines() {
shell.setLayout(new GridLayout(2, true));
(new Label(shell, SWT.NULL)).setText("SWT.BORDER |\nSWT.MUTLI");
(new Label(shell, SWT.NULL)).setText("SWT.BORDER |\nSWT.MUTLI |\nSWT.WRAP");
init();
GridData gridData = new GridData(GridData.FILL_BOTH);
text1.setLayoutData(gridData);
gridData = new GridData(GridData.FILL_BOTH);
text2.setLayoutData(gridData);
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new WrapLines();
}
}