Java Swing does not display Unicode character

Viewed 699

I am working on a Java Swing project and I would like to add a "play/pause" button.
I looked for its Unicode symbol, and got its Java representation which is \u23EF.

So I tried the following:

JButton button = new JButton("\u23EF");

The problem is that the button is blank, the character is not displayed.

In theory, Java with Swing should be able to display Unicode characters exactly like this and without the help of any conversion or anything. I did some research and haven't found any solution.

Why isn't my program behaving correctly? Is it simply a UI problem or did is my code missing something?

2 Answers

you need to change the encoding of your java project. if you are using eclipse IDE then you can write click on project folder and go to properties and in resource section find the Text file encoding and here you have to change it to UTF-8:

eclipse project properties change to this: after chaninging the text file encoding

System.out.println('\u23EF');

output: ⏯

if you are trying to run your file from cmd:

then this link will be helpful for you:

Setting the default Java character encoding

java -Dfile.encoding=UTF-8 … com.x.Main

you can change the encoding of workspaces in eclipse:

change workspace files encoding

and if you are using IntelliJ IDE then follow this link:

configuring-individual-file-encoding

  1. Be sure that your font support this unicode symbol and declare it like :

    Font font = new Font("HereIsTheNameOfYourFont", Font.PLAIN, 24);

  2. Then initialize the button

    JButton button = new JButton("\u23EF");

  3. Finally set the font to button

    button.setFont(font);

Ok, you are ready to add it to your JFrame then.

Related