I have an application where I need to change font sizes frequently. A question posted a year ago on this forum (Change just the font size in SWT) gave me some of the information I needed, but I’ve still got some unknowns I haven’t figured out yet.
In particular, someone signing as hudsonb offered a helpful code fragment which I’d like to reproduce below:
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);
}
});
Suppose I used code like this to change font sizes frequently. Aren’t I creating a whole sequence of DisposeListeners, and adding them to the label’s listener queue? Don’t I need to remove the previous listener each time before adding a new listener? Or is there some mechanism I don’t understand that makes this unnecessary?
An alternative would be to add a
DisposeListenerthat maintains a list of Fonts it needs to dispose of when finished with, e.g.This may not suit your particular application but having something that maintains a list of Fonts is probably a reasonable start. You could also use an anonymous DisposeListener that calls a method in whatever code manages the Font creation/changing/disposal:
It all depends on whether you are creating new labels or not, and whether you want to dispose of Fonts immediately or only when the UI is disposed of.