简体   繁体   中英

FontRegistry and Dispose in SWT (JFace)

I'm learning how to use the FontRegistry and how it works. At the moment I have a centralized class (FontUtils) that at startup load all the font data like this:

fontRegistry.put("small",new FontData[]{new FontData("Tahoma",8, SWT.BOLD)});

Later on in my program I use this:

gc.setFont(FontUtils.getFont("small"));

I was wondering if I have to dispose the font created in the FontUtils, because basing on the documentation every time you create a font you should dispose it. But FontRegistry doesn't know anything about the creation of the font, indeed it doesn't have a method called dispose. Do I have to take care of the dispose by myself?

Fore instance using a variable that counts how many resources are using the font and if it's 0 dispose the font?

Because I'm going to do the same for ImageRegistry I thing the behavior is the same , right?

FontRegistry manages all the Fonts that it owns and deals with disposing of them.

The JavaDoc says:

A font registry owns all of the font objects registered with it, and automatically disposes of them when the SWT Display that creates the fonts is disposed. Because of this, clients do not need to (indeed, must not attempt to) dispose of font objects themselves.

This is done using the disposeExec method of Display .

ImageRegistry is the same:

An image registry owns all of the image objects registered with it, and automatically disposes of them when the SWT Display that creates the images is disposed. Because of this, clients do not need to (indeed, must not attempt to) dispose of these images themselves.

ImageRegistry does also have a dispose method if you want to dispose of images earlier.

Note: It is possible to create an ImageRegistry using your own ResourceManager , in this case it is up to the resource manager to arrange disposal.

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