0

I have the following code :

  JButton Get_Unicode_Button(String unicodeText)
  {
    JButton button=new JButton("\\u"+unicodeText);
//    JButton button=new JButton("\u2605");
//    JButton button=new JButton("\u267b");
//    JButton button=new JButton("\u1F602");  // ?
    return button;
  }

I want to get a button displaying an image from unicode, I have a list of unicodes like this : "2605", "267b", "1F602", but it seems the way I implemented it above doesn't work, what's the right way to do it ?

Especially the 3rd line "\u1F602", even if I hard code it like above, it won't work, why ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frank
  • 30,590
  • 58
  • 161
  • 244

1 Answers1

0

OK, I got it :

  JButton Get_Unicode_Button(String unicodeText)
  {
    int emojiCodePoint=Integer.parseInt(unicodeText,16);
    String emojiAsString=new String(Character.toChars(emojiCodePoint));
    JButton button=new JButton(emojiAsString);
    return button;
  }
Frank
  • 30,590
  • 58
  • 161
  • 244
  • Why are you converting your String to bytes and back to a String again? As soon as you do `new String(Character.toChars(emojiCodePoint))`, you already have the correct String. There is no reason whatsoever to involve bytes. – VGR Jul 05 '19 at 04:34