Java Tutorial/SWT/Text
Содержание
- 1 Append line to Text
- 2 Change Text Font
- 3 Create a multiple-line text field
- 4 Create a password text field
- 5 Create a read-only text field
- 6 Create a right-aligned single-line text field
- 7 Create a single-line text field
- 8 Draw on the background of Text
- 9 Get Caret Position
- 10 Introducing Text and Text Styles
- 11 Resize a text control
- 12 Select all text in Text
- 13 Set size for Text
- 14 Set the selection (start, end)
- 15 Text: type in one text, output to another
Append line to Text
<source lang="java">
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 TextAppendLine {
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(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Change Text Font
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class FontTextChange {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Text text = new Text(shell, SWT.MULTI|SWT.BORDER); text.setBounds(10, 10, 150, 150); Font initialFont = text.getFont(); FontData[] fontData = initialFont.getFontData(); for (int i = 0; i < fontData.length; i++) { fontData[i].setHeight(24); } Font newFont = new Font(display, fontData); text.setFont(newFont); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } newFont.dispose(); display.dispose(); }
}</source>
Create a multiple-line text field
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class CreateMultipleLineText {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); // 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(); }
}</source>
Create a password text field
<source lang="java">
import org.eclipse.swt.SWT; 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 PasswordFieldTextExample {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false));
// Create a password text field new Text(shell, SWT.PASSWORD | SWT.BORDER);
shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Create a read-only text field
<source lang="java">
import org.eclipse.swt.SWT; 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 ReadOnlyTextExample {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false));
// Create a read-only text field new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");
shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Create a right-aligned single-line text field
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class RightalignTextExample {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false));
// Create a right-aligned single-line text field new Text(shell, SWT.RIGHT | SWT.BORDER);
shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Create a single-line text field
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class SingleLinefieldTextExample {
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);
shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Draw on the background of Text
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.TraverseEvent; import org.eclipse.swt.events.TraverseListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Rectangle; 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 TextBackgroundDraw {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER); text.setText("press tab to draw"); text.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) { e.doit = true; GC gc = new GC(text); // Erase background first. Rectangle rect = text.getClientArea(); gc.fillRectangle(rect.x, rect.y, rect.width, rect.height); Font font = new Font(display, "Arial", 32, SWT.BOLD); gc.setFont(font); gc.drawString("tab event", 15, 10); gc.dispose(); } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } }
}</source>
Get Caret Position
<source lang="java">
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 CaretPosition {
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); text.append("Line \n"); text.setSelection(30); System.out.println("selection=" + text.getSelection()); System.out.println("caret position=" + text.getCaretPosition()); System.out.println("caret location=" + text.getCaretLocation()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Introducing Text and Text Styles
SWT offers the Text class for text-entry fields.
StyleDescriptionSWT.MULTICreates a multiple-line text field.SWT.SINGLECreates a single-line text field. This is the default. You may specifiy only one of SWT.MULTI or SWT.SINGLE.SWT.READ_ONLYCreates a text field with uneditable contents.SWT.WRAPWith multiple-line text fields, causes text to wrap.SWT.BORDERDraws a border around the text field. Note that this style isn"t set by default, and your text fields will look funny without it.SWT.CENTERCenters the text in this text field.SWT.LEFTLeft-aligns the text in this text field. This is the default.SWT.RIGHTRight-aligns the text in this text field. You may specify only one of SWT.CENTER, SWT.LEFT, or SWT.RIGHT.SWT.PASSWORDCreates a text field suitable for password entry-it doesn"t display the actual characters the user types, but rather it displays asterisks.SWT.H_SCROLLCreates a horizontal scrollbar to scroll this text field.SWT.V_SCROLLCreates a vertical scrollbar to scroll this text field.
SWT.SINGLE | SWT.LEFT is set by default
Resize a text control
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
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 TextControlResize {
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(); }
}</source>
Select all text in Text
<source lang="java">
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 TextTextSelection {
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); t.setText("here is some text to be selected"); t.selectAll(); shell.setSize(200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Set size for Text
<source lang="java">
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TextSetSize {
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); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Set the selection (start, end)
<source lang="java">
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 SelectionStartEnd {
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, 400, 100); text.append("text text text text text text text text text text text text text "); shell.open(); text.setSelection(30, 38); System.out.println("selection=" + text.getSelection()); System.out.println("caret position=" + text.getCaretPosition()); System.out.println("caret location=" + text.getCaretLocation()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Text: type in one text, output to another
<source lang="java">
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.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TextLink {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Text text0 = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); final Text text1 = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); text0.addVerifyListener(new VerifyListener() { public void verifyText(VerifyEvent event) { text1.setTopIndex(text0.getTopIndex()); text1.setSelection(event.start, event.end); text1.insert(event.text); } }); shell.setBounds(10, 10, 200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>