-3

Problem: I have java code that i would like to display a Unicode character like the following capture image. I succeed to display in System.out.println but in java swing I could not display the character.

Question: How can I display the Unicode character in JTextPane so I can see the emotions?

Capture Image

Code:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    System.out.println(String.valueOf("\u2622"));
    textPane.setContentType("text/html");
    textPane.setText("c'est " + String.valueOf("\u2622"));
    frame.getContentPane().add(textPane, BorderLayout.CENTER);
}
achrefkh
  • 11
  • 1
  • 1
  • 1
    Post pictures _and_ code, not pictures _of_ code. – trashgod Nov 02 '16 at 00:16
  • 1
    Be sure to use a `Font` that can display the character of interest! For that you might use `font.canDisplay(codePoint)` as seen in [this answer](http://stackoverflow.com/a/18858313/418556). – Andrew Thompson Nov 02 '16 at 21:04

1 Answers1

2

Your code works fine, as far as I can tell, you just need to pack() and setVisible(true) in the last lines:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    System.out.println(String.valueOf("\u2622"));
    textPane.setContentType("text/html");
    textPane.setText("c'est " + String.valueOf("\u2622"));
    frame.getContentPane().add(textPane, BorderLayout.CENTER);
    frame.pack();            // Add these
    frame.setVisible(true);  // two lines
}

This should display the JTextPane as you expected.

  • Sorry , I appreciate your help but i added your 2 lines but i steel not solve my problem so i would like to display the caratere Unicode ☢ =\u2622 in jtextpane not like show me this caractere □ in the jtextpane. That is the caractere unknow when it display like this □. Cordially – achrefkh Nov 02 '16 at 22:05
  • No worries, I'm happy to try and help you. Try changing the line with `setContentType` to `textPane.setContentType("text/html; charset=UTF-8");` – chickity china chinese chicken Nov 02 '16 at 22:50
  • Thank you it work but when i try another caractere Unicode like this = \u1F604 they don't show me the result. It seems like that □4. So i tried to change UTF-8 to UTF-16 but steel not work. How can i solve the problem. – achrefkh Nov 02 '16 at 23:14
  • Sorry, I don't know how to get those unicode characters. You probably should ask that on a new question – chickity china chinese chicken Nov 03 '16 at 00:32