Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode Character uncompatibility?

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.

like image 412
Francesco Sollazzi Avatar asked Mar 16 '26 04:03

Francesco Sollazzi


2 Answers

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:

  • Font.canDisplay(char)
  • Font.canDisplay(int)
  • Font.canDisplayUpTo(String)
  • and others.. see docs for details on all of them.

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);
    }
}
like image 142
Andrew Thompson Avatar answered Mar 18 '26 17:03

Andrew Thompson


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.

like image 29
Mick Mnemonic Avatar answered Mar 18 '26 16:03

Mick Mnemonic