Java setting default font

Class Font

The Font class represents fonts, which are used to render text in a visible way. A font provides the information needed to map sequences of characters to sequences of glyphs and to render sequences of glyphs on Graphics and Component objects.

Characters and Glyphs

A character is a symbol that represents an item such as a letter, a digit, or punctuation in an abstract way. For example, ‘g’ , LATIN SMALL LETTER G, is a character.

A glyph is a shape used to render a character or a sequence of characters. In simple writing systems, such as Latin, typically one glyph represents one character. In general, however, characters and glyphs do not have one-to-one correspondence. For example, the character ‘á’ LATIN SMALL LETTER A WITH ACUTE, can be represented by two glyphs: one for ‘a’ and one for ‘´’. On the other hand, the two-character string «fi» can be represented by a single glyph, an «fi» ligature. In complex writing systems, such as Arabic or the South and South-East Asian writing systems, the relationship between characters and glyphs can be more complicated and involve context-dependent selection of glyphs as well as glyph reordering. A font encapsulates the collection of glyphs needed to render a selected set of characters as well as the tables needed to map sequences of characters to corresponding sequences of glyphs.

Читайте также:  Php read directory content

Physical and Logical Fonts

Physical fonts are the actual font libraries containing glyph data and tables to map from character sequences to glyph sequences, using a font technology such as TrueType or PostScript Type 1. All implementations of the Java Platform must support TrueType fonts; support for other font technologies is implementation dependent. Physical fonts may use names such as Helvetica, Palatino, HonMincho, or any number of other font names. Typically, each physical font supports only a limited set of writing systems, for example, only Latin characters or only Japanese and Basic Latin. The set of available physical fonts varies between configurations. Applications that require specific fonts can bundle them and instantiate them using the createFont method.

Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries. Instead, the logical font names are mapped to physical fonts by the Java runtime environment. The mapping is implementation and usually locale dependent, so the look and the metrics provided by them vary. Typically, each logical font name maps to several physical fonts in order to cover a large range of characters.

Peered AWT components, such as Label and TextField , can only use logical fonts.

For a discussion of the relative advantages and disadvantages of using physical or logical fonts, see the Physical and Logical Fonts in The Java Tutorials document.

Font Faces and Names

A Font can have many faces, such as heavy, medium, oblique, gothic and regular. All of these faces have similar typographic design.

Читайте также:  Python in operator dictionary

There are three different names that you can get from a Font object. The logical font name is simply the name that was used to construct the font. The font face name, or just font name for short, is the name of a particular font face, like Helvetica Bold. The family name is the name of the font family that determines the typographic design across several faces, like Helvetica.

The Font class represents an instance of a font face from a collection of font faces that are present in the system resources of the host system. As examples, Arial Bold and Courier Bold Italic are font faces. There can be several Font objects associated with a font face, each differing in size, style, transform and font features.

Glyphs may not always be rendered with the requested properties (e.g, font and style) due to platform limitations such as the absence of suitable platform fonts to implement a logical font.

The getAllFonts method of the GraphicsEnvironment class returns an array of all font faces available in the system. These font faces are returned as Font objects with a size of 1, identity transform and default font features. These base fonts can then be used to derive new Font objects with varying sizes, styles, transforms and font features via the deriveFont methods in this class.

Font and TextAttribute

Font supports most TextAttribute s. This makes some operations, such as rendering underlined text, convenient since it is not necessary to explicitly construct a TextLayout object. Attributes can be set on a Font by constructing or deriving it using a Map of TextAttribute values.

  • FOREGROUND and BACKGROUND use Paint values. The subclass Color is serializable, while GradientPaint and TexturePaint are not.
  • CHAR_REPLACEMENT uses GraphicAttribute values. The subclasses ShapeGraphicAttribute and ImageGraphicAttribute are not serializable.
  • INPUT_METHOD_HIGHLIGHT uses InputMethodHighlight values, which are not serializable. See InputMethodHighlight .

Clients who create custom subclasses of Paint and GraphicAttribute can make them serializable and avoid this problem. Clients who use input method highlights can convert these to the platform-specific attributes for that highlight on the current platform and set them on the Font as a workaround.

The Map -based constructor and deriveFont APIs ignore the FONT attribute, and it is not retained by the Font; the static getFont(java.util.Map) method should be used if the FONT attribute might be present. See TextAttribute.FONT for more information.

Several attributes will cause additional rendering overhead and potentially invoke layout. If a Font has such attributes, the hasLayoutAttributes() method will return true.

Note: Font rotations can cause text baselines to be rotated. In order to account for this (rare) possibility, font APIs are specified to return metrics and take parameters ‘in baseline-relative coordinates’. This maps the ‘x’ coordinate to the advance along the baseline, (positive x is forward along the baseline), and the ‘y’ coordinate to a distance along the perpendicular to the baseline at ‘x’ (positive y is 90 degrees clockwise from the baseline vector). APIs for which this is especially important are called out as having ‘baseline-relative coordinates.’

Источник

How do I get the default font for Swing JTabbedPane labels?

Does the text in Swing components have a default font? In particular, what about tab labels on JTabbedPane s? I’m working on a mock-up of a GUI made with Swing and want it to blend it with a screen image I grabbed of a Swing app.

6 Answers 6

It depends on the Look and Feel. If it’s an application you’ve written, get the values from UIManager.getDefaults().getFont(«TabbedPane.font»)

Yes, thanks, that worked, although you forgot a pair of parentheses: UIManager.getDefaults().getFont(«TabbedPane.font») It turned out to be Arial Bold, as I thought (for what it’s worth).

You can get the font for other components by changing ‘TabbedPane’ to the element type you’re interested in. For instance, I just found that the ‘ToolTip.font’ is ‘Dialog’.

The UIManager Defaults shows what the values are for all properties for all components (including «TabbedPane.font»).

Based on the answer of Reverend Gonzo, this piece of code lets you know what keys are in the UIDefaults. As the keys are self-explanatory, you know what key you can use. I had to know the key for the JTextField font, for example, and could only find it this way.

  • Menu.font=.
  • TextField.font=.
  • RadioButtonMenuItem.font=.
  • ToolTip.font=.
  • TitledBorder.font=.
  • .
  • TabbedPane.font=.
  • .

And thus you would need to pick TabbedPane.font.

Tried this on Java8 but only got a reduced subset of items. Changed the approach to assign the result of getDefaults() to a Hashtable , created an iterator and then checked its keys just as you suggested.

Источник

Set default font for SWT Shell

Is there a way to set a default font for the whole Shell such that any new control will use that same font? It seems that right now I have to set the font for every control I create, which leads to too much redundancy.

2 Answers 2

Font which is used by default is chosen by platform (see other info in Class Font — SWT: The Standard Widget Toolkit), so it’s not possible to set default font for all widgets, if you want that, you have to do it «by hand»..

Why are you changing default font anyway.

Sorry for not being clear enough, but I was interested in changing the font size. The default one is too small and I’d like to have a pref that lets me modify it at will for the whole application.

I’m afraid, that it’s the same thing.. In SWT you set font size by setting new font, with different size..

There was not an api to change the default font size.

However, we could use class shadowing side effect to do that. For example (osx), duplicated a source code org.eclipse.swt.internal.cocoa.NSFont and update the method

public static NSFont systemFontOfSize(double fontSize) < // hacking it by class shadowing fontSize = 24; long result = OS.objc_msgSend(OS.class_NSFont, OS.sel_systemFontOfSize_, fontSize); return result != 0 ? new NSFont(result) : null; >

We re-assign a bigger font size before SWT get the result

My Bigger Font Window

It is hacking, not recommended using at the production evnironment.

Источник

How to change font in Java gui application?

I wanted to change the font in my Java GUI application. Currently I am using Font newFont = new Font(«Serif», Font.BOLD, 24);. To change the font. How can I use fonts like ‘comic sans’ or ‘calibri’ in my GUI based application in Java ? Currently I am using jdk 1.60.

2 Answers 2

You could start by listing the available font names using something like.

String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 0; i

This a great little way of checking the font names.

If you did this, you would find that «Comic Sans» is listed as «Comic Sans MS», this means you’ll need to use something like new Font(«Comic Sans MS», Font.PLAIN, 24) to create a new font

Font

Showing: Comic Sans MS, Calibri, Look and feel default

public class TestPane extends JPanel < public TestPane() < setLayout(new GridBagLayout()); JLabel label = new JLabel("Hello"); label.setFont(new Font("Comic Sans MS", Font.PLAIN, 24)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; add(label, gbc); label = new JLabel("Hello"); label.setFont(new Font("Calibri", Font.PLAIN, 24)); add(label, gbc); label = new JLabel("Hello"); Font font = label.getFont(); label.setFont(font.deriveFont(Font.PLAIN, 24f)); add(label, gbc); >@Override public Dimension getPreferredSize() < return new Dimension(200, 200); >> 

Источник

Оцените статью