3

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?

amadsalmon
  • 56
  • 8
  • Do you know that symbol actually exists in the specific font your button is using? The usual non-display issue is with the font (though generally there's a substitute, such as the empty-box symbol) – user14644949 Nov 23 '20 at 18:02
  • Check out: https://stackoverflow.com/a/29028982/131872 Change the "text" variable to contain the unicode value you are attempting to display and you should get a list of fonts that can display the character. Then you change the Font on your button. – camickr Nov 23 '20 at 18:11

2 Answers2

0

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

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
0
  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.

Stefanidis
  • 61
  • 1
  • 6