SWT – OS agnostic way to get monospaced font

I spent a while bashing my head against this one until I realised that obviously eclipse must have access to a monospace font for use in its text fields, console etc. A little digging turned up:

Font terminalFont = JFaceResources.getFont(JFaceResources.TEXT_FONT);

Which works if all you’re interested in is getting hold of some monospace font.

Edit: Or based on @ctron’s comment:

Font font = JFaceResources.getTextFont();

Edit: Caveat (based on @Lii’s comment): this will get you the configured Text Font, which can be overriden by the user and may not be a monospace font. However, it will be consistent with, e.g., the font used in the editor and console which is probably what you want.

Leave a Comment