java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow Eclipse arrow How to add colored border around the Text Widgets
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4097
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to add colored border around the Text Widgets E-mail
User Rating: / 19
PoorBest 

In general, SWT Widgets does not provide any functionality to add the colored border around it. But this can be done by using a simple trick. Here is the simple example of the Text Widgets to explain the approach.

To add colored border around the Text a Rectangle can be drawn around the Text by using PaintListener().


Image

Implementation Code:

1. private void initialize() {
2.   GridData g=new GridData();
3.  g.grabExcessHorizontalSpace=true;
4.  g.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
5.  Label label=new Label(this,SWT.NONE);
6.  label.setText("Bordered Text");
7.  text = new Text(this, SWT.NONE);
8.  text.setBounds(new org.eclipse.swt.graphics.Rectangle(92,40,84,28));
9.  text.setLayoutData(g);
10.  addPaintListener(new PaintListener() {
11.    public void paintControl(PaintEvent e) {
12.      GC gc=e.gc;
13.      Color red=new Color(null,255,0,0);
14.      gc.setBackground(red);
15.      Rectangle rect=text.getBounds();
16.      Rectangle rect1 = new Rectangle(rText.x-1, rText.y-1
17.        rText.width+2, rText.height+2);
18.
19.      gc.fillRectangle(rect1);
20.      addControlListener(new ControlAdapter() {
21.        public void controlResized(ControlEvent e) {
22.          super.controlResized(e);        21.        }
23.      });
24.    }
25  });
26.}

In the Above code first SWT.Text is added on the composite (In step 7-9), Then Draw the Border around the Text. For this first the boundary of the text is calculated in the form of rectangle (In step 15) then a rectangle with the desired color is drawn to the boundary of the Text (In step 16-19). ControlListener() can be added so that when the size of the Widget change, size of the surrounding rectangle will also change.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.