It is preferred to set the style of the text to be changed to SWT. None. It is strange to set it to SWT. Border.
However, if it is set to SWT. None, there is no border for text. The display is also strange, and the boundary is gray-white. In a container with a lighter background color, the border and range of text are almost invisible.
There is a compromise solution, that is, to simulate drawing a boundary for the text on the container where the text is placed.
Eg:
Text. addpaintlistener (New paintlistener (){
Public void paintcontrol (paintevent E)
{
Rectangle recttmp = text. getbounds ();
Point size = new point (recttmp. Width, recttmp. Height );
Final int [] pointarray = new int [] {0, 2, 0, size. x-2, 0, size. x, 2, size. x, size. y-2, size. x-2, size. y, 2, size. y, 0, size. y-2, 0, 2 };
Region region = new region ();
Region. Add (pointarray );
Text. setregion (region );
Color bordercolor = new color (display. getdefault (), new RGB (202,202,204 ));
GC = new GC (text. getparent ());
GC. setforeground (bordercolor );
// Draw the border for text
Final int [] pointarray2 = new int [] {recttmp. x-1, recttmp. Y + 2, 2 + recttmp. x, 0 + recttmp. y-1, recttmp. X + size. x-2, 0 + recttmp. y-1, size. X + recttmp. x, 2 + recttmp. y, size. X + recttmp. x, size. y-2 + recttmp. y, size. x-2 + recttmp. x-1, size. Y + recttmp. y, 2 + recttmp. x-1, size. Y + recttmp. y, 0 + recttmp. x-1, size. y-2 + recttmp. y, 0 + recttmp. x-1, 2 + recttmp. y };
GC. drawpolyline (pointarray2 );
// Release static Resources
Bordercolor. Dispose ();
GC. Dispose ();
Region. Dispose ();
} S
});
Note:
Final int [] pointarray2 = new int [] {recttmp. x-1, recttmp. Y + 2, 2 + recttmp. x, 0 + recttmp. y-1, recttmp. X + size. x-2, 0 + recttmp. y-1, size. X + recttmp. x, 2 + recttmp. y, size. X + recttmp. x, size. y-2 + recttmp. y, size. x-2 + recttmp. x-1, size. Y + recttmp. y, 2 + recttmp. x-1, size. Y + recttmp. y, 0 + recttmp. x-1, size. y-2 + recttmp. y, 0 + recttmp. x-1, 2 + recttmp. y };
GC. drawpolyline (pointarray2 );
Some coordinates of the pointarray2 array may need to be fine-tuned.
Another method is to use canvas to wrap a text file. Fill text with canvas as much as possible. At the same time, draw a rounded border on the canvas boundary.
Eg:
// Text input box Control
Public class ctext extends canvas implements listener {
Private text;
Private color outercolor = getparent (). getbackground ();
Private color bordercolor = new color (display. getdefault (), new RGB (255, 0 ));
// Private color bordercolor = new color (display. getdefault (), new RGB (202,202,204 ));
Public ctext (composite parent, int style ){
Super (parent,/* SWT. Border | */SWT. double_buffered );
Gridlayout GL = new gridlayout ();
Gl. marginwidth = gl. marginheight = 0;
Gl. margintop = gl. marginbottom = 1;
Gl. marginleft = gl. marginright = 1;
This. setlayout (GL); // grid layout, full
TEXT = new text (this, SWT. None/* | SWT. Border */);
Text. setlayoutdata (New griddata (griddata. fill_both ));
This. addlistener (SWT. Paint, this );
This. addlistener (SWT. Dispose, this );
}
// Handle event
Public void handleevent (event ){
Switch (event. Type ){
Case SWT. Paint: onpaint (event); break;
Case SWT. Dispose: ondisposed ();
}
}
// Paint
Private void onpaint (event e ){
Rectangle recttmp = text. getbounds ();
Point size = new point (recttmp. Width, recttmp. Height );
Final int [] pointarray = new int [] {0, 2, 0, size. x-2, 0, size. x, 2, size. x, size. y-2, size. x-2, size. y, 2, size. y, 0, size. y-2, 0, 2 };
Region region = new region ();
Region. Add (pointarray );
Text. setregion (region );
E. gc. setbackground (bordercolor );
E. gc. setforeground (bordercolor );
Final int [] pointarray2 = new int [] {0, 2, 0, size. x-2 +, size. X + 1, 2, size. X + 1, size. y-2 + 1, size. x-2 + 1, size. Y + 1, 2, size. Y + 1, 0, size. y-2 + 1, 0, 2 };
// E. gc. fillpolygon (pointarray2 );
E. gc. drawpolyline (pointarray2 );
// Graphicutils. drawroundrectangle (GC, rect. X, rect. Y, rect. Width-1, rect. Height, outercolor, bordercolor, true, true );
}
// Disposed
Private void ondisposed (){
If (outercolor! = NULL &&! Outercolor. isdisposed ()){
Outercolor. Dispose ();
Outercolor = NULL;
}
If (bordercolor! = NULL &&! Bordercolor. isdisposed ()){
Bordercolor. Dispose ();
Bordercolor = NULL;
}
}
Public void settext (string textvalue ){
Text. settext (textvalue = NULL? "": Textvalue );
}
Public String gettext (){
If (this. isdisposed ()){
Return "";
}
Return text. gettext ();
}
}
One drawback of this method is that when you need some native methods of text, you must provide most of the text method interfaces for the classes inherited from canvas.
A compromise is that the encapsulated text instance is returned in the subclass of canvas.
Eg:
Public class ctext extends canvas implements listener {
Private text;
// Other code...
Public text gettextcontrol (){
Return this. text;
}
}