12

Does the text in Swing components have a default font? In particular, what about tab labels on JTabbedPanes?

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.

Ky.
  • 28,753
  • 48
  • 180
  • 285
Paul Reiners
  • 9,480
  • 31
  • 110
  • 189

6 Answers6

21

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

Brad Mace
  • 26,397
  • 17
  • 98
  • 144
Reverend Gonzo
  • 37,961
  • 6
  • 57
  • 77
  • 1
    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). – Paul Reiners Sep 16 '09 at 19:27
  • 3
    How do you determine the appropriate keys for other swing components? – Brad Mace Feb 10 '12 at 22:30
  • 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'. – marklark Oct 06 '16 at 19:28
3

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

camickr
  • 316,400
  • 19
  • 155
  • 279
3

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.

Set<Object> keys = UIManager.getDefaults().keySet();
for (Object key : keys) {
     if (key instanceof String && ((String) key).contains("font")) {
          System.out.println(key + "=" + UIManager.getDefaults().get(key));
     }
}

If you're looking for a font, in your case, just cast the key to a String and check whether it contains the word "font". This way you narrow the set of keys you have potential interest for.

I got a list

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

And thus you would need to pick TabbedPane.font.

Timmos
  • 3,084
  • 2
  • 30
  • 39
  • 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. – Alfabravo Apr 04 '16 at 15:40
1

It may depend on the 'Look and Feel' you are using, but for me Swing's default font is

DejaVu Sans - Plain

For most components the font size defaults to around 12 or 13

Tom Neyland
  • 6,769
  • 2
  • 35
  • 52
0

It looks like it's Arial. That's what Identifont tells me and it looks right.

Paul Reiners
  • 9,480
  • 31
  • 110
  • 189
-3

The Java GUI default font is "Helvetica", bold size 9, color gray.

  • that's wrong in its absoluteness: as others already correcty answered, fonts depend on LAFs and/or OS setting – kleopatra Mar 30 '13 at 10:31