简体   繁体   中英

How can I add a solid line border to a label in SWT and change the color of the border?

I want to stylize a standard SWT Label by giving it a solid border and picking a color for that border. I can't seem to find a way to do that by looking it up online. Is that even supported? All I can seem to do is just apply the SWT.BORDER style to it, which is a 3D effect border that I seemingly have little control over.

Add a PaintListener to your Label. On this Listener's paintControl method, get the generated event's GC and, with it, draw a rectangle with the same shape as your label.

You can, in fact, draw anything over your Label.

JFace Forms provide that in the FormToolkit . See here .

The private class BorderPainter (lines 106-165) does what Mario already described in his answer: custom painting a border around your widget. In order to make it work, you have to add the BorderPainter, which is a PaintListener , to your widget's parent, see paintBordersFor(.) in line 798.

I wouldn't recommend adding jface forms as a dependency if you need just the border painter - way too heavy-weight. But that code should help you along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM