Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » highlight or color the border of SWT widget
highlight or color the border of SWT widget [message #489612] Mon, 05 October 2009 10:42 Go to next message
jayant  is currently offline jayant Friend
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 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
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)
>
>
Re: highlight or color the border of SWT widget [message #489704 is a reply to message #489688] Mon, 05 October 2009 15:39 Go to previous messageGo to next message
jayant  is currently offline jayant Friend
Messages: 4
Registered: October 2009
Junior Member
Thanks Grant for the solution ..this wud help me greatly..
I also happen to see another solution in

http://www.java-tips.org/other-api-tips/eclipse/how-to-add-c olored-border-around-the-text-wi-2.html

but the code snippet does not work.maybe somebody can throw some light on this example and make this workable.
Re: highlight or color the border of SWT widget [message #489921 is a reply to message #489612] Tue, 06 October 2009 14:00 Go to previous message
jayant  is currently offline jayant Friend
Messages: 4
Registered: October 2009
Junior Member
Another solution i found out was :-

public class Test {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Text text = new Text(shell, SWT.NONE);
text.setText("string");
GridDataFactory.fillDefaults().grab(true, false).applyTo(text);
shell.open();
GC gc = new GC(shell);
gc.setLineWidth(2);
gc.setForeground(Display.getDefault().getSystemColor(SWT.COL OR_RED));
gc.drawRectangle(text.getBounds());
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Previous Topic:Convert pixels to mm
Next Topic:Drag & Drop Behavior Different on Mac?
Goto Forum:
  


Current Time: Thu Apr 25 01:25:56 GMT 2024

Powered by FUDForum. Page generated in 0.03366 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top