Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Calculate with pixels of text widget
Calculate with pixels of text widget [message #522217] Sun, 21 March 2010 12:19 Go to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
Hello.

I want to calculate and set the width of a text widget.
I know that the widget can contain a maximum of 5 digits using a grid layout. So I want to use the width hint. But how could I calculate the correct pixel count?

Regards,
Kai
Re: Calculate with pixels of text widget [message #522303 is a reply to message #522217] Mon, 22 March 2010 08:33 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 21.03.2010 13:19, Kai Schlamp wrote:
> I want to calculate and set the width of a text widget.
> I know that the widget can contain a maximum of 5 digits using a grid
> layout. So I want to use the width hint. But how could I calculate the
> correct pixel count?

I think you may want to create a GC on the fly, call either of
stringExtent or textExtend (Note the differences in the
documentation) given a specific text and dispose the GC immediately
as in:

GC gc = new GC(control);
Point size = stringExtent("12345"); // or textExtent
your.widthHint = size.x;
gc.dispose();

Alternatively you can get a good estimate via the FontMetrics:

GC gc = new GC(control);
fontMetrics = gc.getFontMetrics();
gc.dispose();

and then compute (as the jface.Dialog function does)

public static int convertWidthInCharsToPixels(FontMetrics fontMetrics,
int chars) {
return fontMetrics.getAverageCharWidth() * chars;
}

HTH & Greetings from Bremen,

Daniel Krügler
Previous Topic:Embedding custom cocoa widgets in SWT
Next Topic:Composite invisible?
Goto Forum:
  


Current Time: Fri Dec 06 16:51:41 GMT 2019

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

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

Back to the top