2

I have a problem with character encoding using swing on Java. I want to write this character:

"\u2699"

That is a gear on a simple JButton but when I start my program I get only a JButton with a square and not the gear. This is the line:

opt.setText("\u2699");

Where opt is the button.

The button result:

This is the button

Can I change swing character encoding or something else? Thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frank Soll
  • 420
  • 1
  • 4
  • 15
  • 2
    Change to a font that has a glyph for the [⚙](https://www.fileformat.info/info/unicode/char/2699/index.htm) gear character – Andreas Jan 26 '18 at 19:20
  • I always get the same result, also with Calibri or Arial – Frank Soll Jan 26 '18 at 19:25
  • Those are not symbol fonts. Try "Wingding" or something like that. – Andreas Jan 26 '18 at 19:26
  • No, it doesn't work – Frank Soll Jan 26 '18 at 19:29
  • Possible duplicate of [Unicode special characters appearing in Java console, but not in Swing](https://stackoverflow.com/questions/46699135/unicode-special-characters-appearing-in-java-console-but-not-in-swing) – i.karayel Jan 26 '18 at 20:08
  • Ok, I solved the problem. I found a font that supports this character (FreeSerif) and I made a code to install it in GraphicsEnvironment, and so it works! – Frank Soll Jan 28 '18 at 14:47

2 Answers2

6

As mentioned by Andreas, use a Font that supports that character. But unless supplying a suitable font for the app., the Font API provides ways of discovering compatible fonts at run-time. It provides methods like:

In this example, we show the dozen fonts on this system that will display the gear character.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GearFonts {

    private JComponent ui = null;
    int codepoint = 9881; // Unicode codepoint: GEAR
    String gearSymbol = new String(Character.toChars(codepoint));

    GearFonts() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout(0,2,5,5));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        Font[] fonts = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAllFonts();
        for (Font font : fonts) {
            if (font.canDisplay(codepoint)) {
                JButton button = new JButton(gearSymbol + " " + font.getName());
                button.setFont(font.deriveFont(15f));
                ui.add(button);
            }
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                GearFonts o = new GearFonts();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

As @Andreas points out, the button needs to be set to use a font that supports this Unicode value. For sorting out compatibility issues such as this one, fileformat.info is a great resource. Here is the list of fonts known to support the Gear character. These include for example DejaVu Sans and Symbola.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30