tags:

views:

1164

answers:

3

I need to use a larger font for one of the labels.

label.setFont( new Font(display,"Arial", 14, SWT.BOLD ) );

but obviously Arial is not always the default font. I want to change just the size and keep everything else at default values.

Can I do something like

label.setFontSize( 14 );

to avoid setting the other parameters? Or can I at least find out the name of the font that is actually being used as default?

+6  A: 

I believe you could do something like

FontData[] fD = label.getFont().getFontData();
fD[0].setHeight(16);
label.setFont( new Font(display,fD[0]));

As long as no more than one font is returned, that should work.

Mike K
A: 

You must dispose the old one before or after setting a new one. If not, you can get "SWT: No more handles" and your app will crash.

Fraer9
This is incorrect. The old font does not have to be disposed, since he didn't create it. He is responsible for disposing the new font though, since he created it. This article is a good reference: http://www.eclipse.org/articles/swt-design-2/swt-design-2.html
hudsonb
+2  A: 

You can do the following:

FontData[] fontData = label.getFont().getFontData();
for(int i = 0; i < fontData.length; ++i)
    fontData[i].setHeight(14);

final Font newFont = new Font(display, fontData);
label.setFont(newFont);

// Since you created the font, you must dispose it
label.addDisposeListener(new DisposeListener() {
    public void widgetDisposed(DisposeEvent e) {
        newFont.dispose(image);
    }
});
hudsonb