How to add material design icon to JavaFX using codepoint?

Viewed 137

To use material design icons I did the following:

1.Downloaded ttf font from https://materialdesignicons.com/
2.in my css file I added:

  @font-face {
    src: url('fonts/materialdesignicons-webfont.ttf');
  }
  .icon {
    -fx-font-size: 15px;
    -fx-font-family: 'Material Design Icons';
  }

3.Here https://pictogrammers.github.io/@mdi/font/6.5.95/ I copied the first icon ( mdi-account-lock-open with codepoint F1960) and pasted this icon in my Java code:

enter image description here

4.After that I added icon class to button and everything is ok - I have a button with an icon.

The only problem is that in my java code I can't see what icon I use - I have everywhere empty rectangles. If it is possible, I would like to add icon using codepoints, for this example codepoint F1960. I tried

private final Button button = new Button("\uF1960");

but it didn't work. Could anyone say how to do it?

2 Answers

Java characters are UTF-16 values. You cannot directly embed U+F1960 in a String, because Unicode \u escapes must be followed by exactly four hex digits.

Characters with codepoint values above FFFF are called supplementary characters. UTF-16 represents such a character using a surrogate pair, which is a sequence of two characters from a reserved section of the Unicode repertoire.

You can represent U+F1960 in a String literal as its surrogate pair: "\udb86\udd60"

An alternative approach that’s easier to remember is String.format("%c", 0xF1960). The underlying java.util.Formatter is documented as allowing an Integer argument for %c, and unlike characters, an Integer can be larger than 16 bits.

For icons I'd propose to use ikonli. It provides the best integration of a lot of icon fonts into JavaFX that i know.

The specific set you are looking for seems to be this one: materialdesign2

Related