Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Stupid StyledText Trick
Stupid StyledText Trick [message #437255] Wed, 02 June 2004 03:45 Go to next message
Mark Freiheit is currently offline Mark FreiheitFriend
Messages: 30
Registered: July 2009
Member
Hi all...

I am trying to create a simple rectangle with a colored background, with
some text horizontally
aligned to the left, and centered vertically. The StyledText widget
seemed appropriate...

Implementing this code...

StyledText banner = new StyledText(tcComp, SWT.READ_ONLY);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING |
GridData.VERTICAL_ALIGN_CENTER |
GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 5;
gridData.heightHint = 40;
banner.setLayoutData(gridData);

banner.setBackground(ColorHelper.get(ColorHelper.FRAME_HEADE R_BKGND));
banner.setText("xyzzyx");
banner.setStyleRange(
new StyleRange(0, banner.getText().length(), null, null,
SWT.BOLD));

Provides me with a very close solution, but the text is drawn at the top
of the rectangle.
Any thoughts on the vertical alignment? Is there a better approach to
this problem?

Thanks -- Mark
Re: Stupid StyledText Trick [message #437284 is a reply to message #437255] Wed, 02 June 2004 13:47 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
I think you'll find it easier to just draw the stuff:

public static void main(String[] args) {
final String string = "Bold text to vertically center";
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,400,200);
Canvas canvas = new Canvas (shell, SWT.NONE);
canvas.setBounds(10,10,300,100);
FontData[] data = canvas.getFont().getFontData();
for (int i = 0; i < data.length; i++) {
data[i].setStyle(data[i].getStyle() | SWT.BOLD);
}
Font newFont = new Font(display, data);
canvas.setFont(newFont);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
e.gc.fillRectangle(0,0,300,100);
Point size = e.gc.stringExtent(string);
e.gc.drawText(string, 0, (100 - size.y) / 2);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
newFont.dispose();
display.dispose();
}

Grant

"Mark" <freiheit@speakeasy.net> wrote in message
news:c9jih0$jvi$1@eclipse.org...
> Hi all...
>
> I am trying to create a simple rectangle with a colored background, with
> some text horizontally
> aligned to the left, and centered vertically. The StyledText widget
> seemed appropriate...
>
> Implementing this code...
>
> StyledText banner = new StyledText(tcComp, SWT.READ_ONLY);
> gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING |
> GridData.VERTICAL_ALIGN_CENTER |
> GridData.FILL_HORIZONTAL);
> gridData.horizontalSpan = 5;
> gridData.heightHint = 40;
> banner.setLayoutData(gridData);
>
> banner.setBackground(ColorHelper.get(ColorHelper.FRAME_HEADE R_BKGND));
> banner.setText("xyzzyx");
> banner.setStyleRange(
> new StyleRange(0, banner.getText().length(), null, null,
> SWT.BOLD));
>
> Provides me with a very close solution, but the text is drawn at the top
> of the rectangle.
> Any thoughts on the vertical alignment? Is there a better approach to
> this problem?
>
> Thanks -- Mark
>
>
Previous Topic:initial setFocus() for widget in Dialog
Next Topic:UnsatisfiedLinkException (Internal Error 193) in SWT DLL
Goto Forum:
  


Current Time: Fri Apr 26 20:40:45 GMT 2024

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

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

Back to the top