highlight or color the border of SWT widget [message #489612] |
Mon, 05 October 2009 10:42 |
jayant Messages: 4 Registered: October 2009 |
Junior Member |
|
|
I have a task of highlighting or coloring the border of SWT widgets .for example :a SWT Text with border red, i am trying hard to set the border to red by getting the bounds of the swt widgets and set a new rectangle and coloring it by calling the method fillRectangle().Pls check the code below but no sucess ...pls go thru the code snippet (attachment)
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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 SampleView2 {
public static void main(String[] args) {
final Text txtName;
Display display = new Display();
Shell shell = new Shell(display);
// Label positiongLabel = new Label(shell, SWT.BORDER);
// positiongLabel.setBounds(100,50,300,30);
//
// positiongLabel.setText("My Position is : " +
// positiongLabel.getBounds());
//
GridData g = new GridData();
g.grabExcessHorizontalSpace = true;
g.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
Label lblName = new Label(shell, SWT.NONE);
lblName.setText("Customer Name");
lblName.setLayoutData(g);
txtName = new Text(shell, SWT.BORDER);
txtName
.setBounds(new org.eclipse.swt.graphics.Rectangle(200, 50, 300,
28));
txtName.setLayoutData(g);
txtName.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
e.gc.setBackground(new Color(null, 255, 0, 0));
Rectangle rect = txtName.getBounds();
Rectangle rect1 = new Rectangle(rect.x - 1, rect.y - 1, rect.width + 2,
rect.height + 2);
e.gc.fillRectangle(rect1);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
[Updated on: Mon, 05 October 2009 10:44] Report message to a moderator
|
|
|
Re: highlight or color the border of SWT widget [message #489688 is a reply to message #489612] |
Mon, 05 October 2009 15:00 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Hi,
(your attachment doesn't seem to have made it)
There isn't native support for changing a control's border colour, so you
won't be able to, for instance, make a control draw its bezelled border in
red.
The best way for you to draw a border around a Control is to give it an
extra parent Composite and draw the border on the Composite. For example,
the snippet below draws a red border around a Text.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,200);
shell.setLayout(new GridLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setBackground(display.getSystemColor(SWT.COLOR_RED ));
composite.setLayoutData(new GridData(100,100));
FillLayout layout = new FillLayout();
layout.marginHeight = layout.marginWidth = 1;
composite.setLayout(layout);
new Text(composite, SWT.MULTI); // no BORDER bit
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
Grant
"jayant" <jayant.chaudhury@deutschebahn.com> wrote in message
news:hacii6$eep$1@build.eclipse.org...
> I have a task of highlighting or coloring the border of SWT widgets .for
example :a SWT Text with border red, i am trying hard to set the border to
red by getting the bounds of the swt widgets and set a new rectangle and
coloring it by calling the method fillRectangle().Pls check the code below
but no sucess ...pls go thru the code snippet (attachment)
>
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03701 seconds