Monospaced font/symbols for JTextPane
Asked Answered
C

2

14

I want to build a console-like output using JTextPane. Therefore I am using a monospaced font:

textpane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

This works fine for all kind of alphanum (like a-z, 0-9 etc.) characters, but when it comes to symbols like '\u2588' (█), the font isn't monospaced anymore.

Did I forgot something? Or isn't there a monospaced font which includes smybols?

Chancechancel answered 13/6, 2012 at 18:20 Comment(0)
S
16

Ok, first off, it sounds to me like you're trying to address a couple of different things here, so I'll try to address them both separately.

1. You need a font that is monospaced for all unicode characters, symbols or otherwise.

According to this page, there were 12886 alphanumeric and "symbol" characters defined by the Unicode 3.2 standard. Unicode is now at 6.0, so we can probably assume that number is larger now. I'm also assuming here that "alphanumeric" means English characters, because Unicode supports over 100000 characters spanning many languages. At any rate, 12886 English-recognized characters and symbols is still A LOT, and I doubt that there are many free fonts that support all of them.

That said, I end up using Courier New for most of my Java applications that need a mono-spaced font with character support. It supports the '\u2588' character you mentioned above as well as many other important ones like the "degrees" symbol.

2. This mono-spaced font needs to be "cross-platform"

I know for certain that Mac OS X and all flavors of Windows support Courier New, and the versions of Linux I run on do too (RedHat... can't remember the version number), although not all Linux versions have this font natively. Anyway... you might try Courier New and see if it works for you. If not, you can probably find free tools online for testing font/character support.

Final thoughts

I hate to say it, but I doubt that there are many fonts out there that support all alphanumeric/symbol characters defined by the Unicode standard, least of all being mono-spaced, cross-platform, and free. If possible, it might be more worth your time to try to figure out what symbols you will need for certain, then choose a font that supports those symbols and is in turn supported by the platforms you know you will run your application on. If you absolutely need to have support for all Unicode symbols, unfortunately, it probably won't come free.

Sitology answered 13/6, 2012 at 19:23 Comment(2)
With alphanum characters I meant a-Z, 0-9 etc. Courier New worked fine for me.Chancechancel
I reccomend source code pro for a nice free cross platform font. You will either need to bundle it in your installer or have your users install it manually. github.com/adobe-fonts/source-code-pro/releasesContralto
T
3

Monospaced font works on OS X, but not Windows. The weird thing is that monospaced is configured to use Courier New on Windows via the fontconfig.properties.

I've actually logged this as a bug with Oracle; here's some sample code that renders correctly on OS X but the monospaced font produces square boxes on Windows.

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame("editor test");
        f.getContentPane().setLayout(new BorderLayout());
        f.add(pane("monospaced"), BorderLayout.NORTH);
        f.add(pane("Courier New"), BorderLayout.SOUTH);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

  private static JEditorPane pane(final String name) {
    JEditorPane p = new JEditorPane();
    final Font currFont = p.getFont();
    p.setFont(new Font(name, currFont.getStyle(), currFont.getSize()));
    p.setText(name + " - 8\u1d00.\u1d0d.");
    return p;
  }
}

I tried adding \u2588 to it and that looks fine on OS X monospaced too.

Titanomachy answered 14/8, 2012 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.