SWT (JFace) experience of Styledtext class _java programming

Source: Internet
Author: User
Wraplines.java
Copy Code code as follows:

Package Swt_jface.demo4;
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.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 ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New WrapLines ();
}
}

Remarkstext.java
Copy Code code as follows:

Package Swt_jface.demo4;
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");
Shell.pack ();
Shell.setsize (300, 150);
Shell.open ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Remarkstext ();
}
}

Again such as Password box:
Userpassword.java
Copy Code code as follows:

Package Swt_jface.demo4;
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 UserPassword {

Display display = new display ();
Shell shell = new shell (display);

Text Textuser;
Text Textpassword;
private void init () {

(New Label (Shell, SWT.) NULL)). SetText ("User name:");
Textuser = new Text (Shell, SWT. Single | Swt. BORDER);
Textuser.settext ("Default_user");
Textuser.settextlimit (16);
(New Label (Shell, SWT.) NULL)). SetText ("Password:");
Textpassword = new Text (Shell, SWT. Single | Swt. BORDER);
System.out.println (Textpassword.getechochar ());
Textpassword.setechochar (' * ');
}

Public UserPassword () {

Shell.setlayout (New GridLayout (2, false));
Init ();

Textuser.setlayoutdata (New Griddata (griddata.fill_horizontal));
Textpassword.setlayoutdata (New Griddata (griddata.fill_horizontal));

Shell.pack ();
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New UserPassword ();
}
}

Here are some examples of modifying text using the Styledtext class:

Highlightoddline.java
Copy Code code as follows:

Package Swt_jface.demo4;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.custom.LineBackgroundEvent;
Import Org.eclipse.swt.custom.LineBackgroundListener;
Import Org.eclipse.swt.custom.StyledText;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
public class Highlightoddline {

Display display = new display ();
Shell shell = new shell (display);

Styledtext Styledtext;

Public Highlightoddline () {

Shell.setlayout (New GridLayout ());
Styledtext = new Styledtext (Shell, SWT. MULTI | Swt. WRAP | Swt. BORDER | Swt. H_scroll | Swt. V_scroll);
Styledtext.setlayoutdata (New Griddata (Griddata.fill_both));
Styledtext.addlinebackgroundlistener (New Linebackgroundlistener () {
public void Linegetbackground (Linebackgroundevent event) {
if (Styledtext.getlineatoffset (event.lineoffset)% 2 = 1)
Event.linebackground = Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow);
}
});

Styledtext.settext ("line 0\r\nline 1\r\nline 2\r\nline 3\r\nline 4\r\nline 6");
Shell.setsize (300, 150);
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Highlightoddline ();
}
}

Setlinebackground.java
Copy Code code as follows:

Package Swt_jface.demo4;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.custom.StyleRange;
Import Org.eclipse.swt.custom.StyledText;
Import Org.eclipse.swt.graphics.Font;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
public class Setlinebackground {

Display display = new display ();
Shell shell = new shell (display);

Styledtext Styledtext;
Public Setlinebackground () {
Shell.setlayout (New GridLayout ());
Styledtext = new Styledtext (Shell, SWT. BORDER | Swt. MULTI | Swt. WRAP | Swt. H_scroll | Swt. V_scroll);
Styledtext.setlayoutdata (New Griddata (Griddata.fill_both));
Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);
Styledtext.setfont (font);
Styledtext.settext ("ABCDEFG\R\NHIJKLMN");
Stylerange styleRange1 = new Stylerange ();
Stylerange1.start = 2;
Stylerange1.length = 3;
Stylerange1.foreground = Shell.getdisplay (). Getsystemcolor (SWT. Color_blue);
Stylerange1.background = Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow);
Stylerange1.fontstyle = SWT. BOLD;

Styledtext.setstylerange (STYLERANGE1);
Styledtext.setlinebackground (0, 1, shell.getdisplay (). Getsystemcolor (SWT. Color_green));
Styledtext.setlinebackground (1, 1, Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow));

Shell.setsize (300, 120);
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}

public static void Main (string[] args) {
New Setlinebackground ();
}
}

Searchstyletext.java
Copy Code code as follows:

Package Swt_jface.demo4;
Import java.util.LinkedList;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.custom.LineStyleEvent;
Import Org.eclipse.swt.custom.LineStyleListener;
Import Org.eclipse.swt.custom.StyleRange;
Import Org.eclipse.swt.custom.StyledText;
Import Org.eclipse.swt.events.SelectionAdapter;
Import org.eclipse.swt.events.SelectionEvent;
Import Org.eclipse.swt.graphics.Font;
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 Searchstyletext {

Display display = new display ();
Shell shell = new shell (display);
Styledtext Styledtext;
Text Keywordtext;
Button button;

String keyword;

Public Searchstyletext () {

Shell.setlayout (New GridLayout (2, false));
Styledtext = new Styledtext (Shell, SWT. MULTI | Swt. WRAP | Swt. BORDER | Swt. H_scroll | Swt. V_scroll);
Griddata griddata = new Griddata (Griddata.fill_both);
Griddata.horizontalspan = 2;
Styledtext.setlayoutdata (Griddata);
Keywordtext = new Text (Shell, SWT. Single | Swt. BORDER);
Keywordtext.setlayoutdata (New Griddata (griddata.fill_horizontal));
Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);
Styledtext.setfont (font);
button = New button (Shell, SWT. PUSH);
Button.settext ("Search");
Button.addselectionlistener (New Selectionadapter () {
public void widgetselected (Selectionevent e) {
Keyword = Keywordtext.gettext ();
Styledtext.redraw ();
}
});

Styledtext.addlinestylelistener (New Linestylelistener () {
public void Linegetstyle (Linestyleevent event) {
if (keyword = null | | keyword.length () = 0) {
Event.styles = new Stylerange[0];
Return
}
String line = Event.linetext;
int cursor =-1;
LinkedList list = new LinkedList ();
while ((cursor = line.indexof (keyword, cursor+1)) >= 0) {
List.add (Gethighlightstyle (Event.lineoffset+cursor, Keyword.length ()));
}
Event.styles = (stylerange[]) List.toarray (new Stylerange[list.size ()));
}
});

Keyword = "SW";
Styledtext.settext ("AWT, SWING \R\NSWT & JFACE");

Shell.pack ();
Shell.open ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}

Private Stylerange gethighlightstyle (int startoffset, int length) {

Stylerange Stylerange = new Stylerange ();
Stylerange.start = Startoffset;
stylerange.length = length;
Stylerange.background = Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow);
return stylerange;
}
public static void Main (string[] args) {
New Searchstyletext ();
}
}

Samplestyledtext.java
Copy Code code as follows:

Package Swt_jface.demo4;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.custom.StyleRange;
Import Org.eclipse.swt.custom.StyledText;
Import Org.eclipse.swt.graphics.Font;
Import Org.eclipse.swt.layout.GridData;
Import Org.eclipse.swt.layout.GridLayout;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
public class Samplestyledtext {

Display display = new display ();
Shell shell = new shell (display);
Styledtext Styledtext;
Public Samplestyledtext () {

Shell.setlayout (New GridLayout ());
Styledtext = new Styledtext (Shell, SWT. MULTI | Swt. WRAP | Swt. BORDER | Swt. H_scroll | Swt. V_scroll);
Styledtext.setlayoutdata (New Griddata (Griddata.fill_both));
Font font = new Font (Shell.getdisplay (), "Courier new", SWT. NORMAL);
Styledtext.setfont (font);
Styledtext.settext ("123456789\r\nabcdefghi");

Stylerange styleRange1 = new Stylerange ();
Stylerange1.start = 2;
Stylerange1.length = 16;
Stylerange1.foreground = Shell.getdisplay (). Getsystemcolor (SWT. Color_blue);
Stylerange1.background = Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow);
Stylerange1.fontstyle = SWT. BOLD;

Stylerange styleRange2 = new Stylerange ();
Stylerange2.start = 14;
Stylerange2.length = 3;
Stylerange2.fontstyle = SWT. NORMAL;
Stylerange2.foreground = Shell.getdisplay (). Getsystemcolor (SWT. Color_yellow);
Stylerange2.background = Shell.getdisplay (). Getsystemcolor (SWT. Color_blue);

Styledtext.setstylerange (STYLERANGE1);
Styledtext.setstylerange (StyleRange2);
Styledtext.setstyleranges (New Stylerange[]{stylerange1, styleRange2});
Styledtext.setstyleranges (New Stylerange[]{stylerange2, styleRange1});
Styledtext.setlinebackground (1, 1, Shell.getdisplay (). Getsystemcolor (SWT. Color_gray));
Styledtext.setselection (4);
System.out.println (Printstyleranges (Styledtext.getstyleranges ()));
Styledtext.insert ("000");

System.out.println (Printstyleranges (Styledtext.getstyleranges ()));
Styledtext.setstyleranges (New Stylerange[]{stylerange1});
System.out.println (Printstyleranges (Styledtext.getstyleranges ()));

Shell.pack ();
Shell.setsize (300, 120);
Shell.open ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}

Private String printstyleranges (stylerange[] styleranges) {
if (styleranges = null)
return "NULL";
else if (styleranges.length = 0)
Return "[]";
StringBuffer sb = new StringBuffer ();
for (int i=0; i<styleranges.length; i++) {
Sb.append (styleranges[i] + "\ n");
}
return sb.tostring ();
}
public static void Main (string[] args) {
New Samplestyledtext ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.