1

I'm using a JTree to display data on my GUI. Sometimes this data is a string, which is UTF-8 formatted. These strings may contain foreign characters or emojis. My problem is that the strings are display wrong.

Example:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class Demo {
    public static void main(String[] args) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: ");
        root.add(leaf);

        JTree tree = new JTree(root);

        JFrame frame = new JFrame("Demo");
        frame.add(new JScrollPane(tree));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Output:

Screenshot of output

- Root  
|-- []eaf: []

Is it possible to display those strings correctly using a JTree?

For future readers:

Maybe you want to check this, too. Click

Community
  • 1
  • 1
Moritz
  • 198
  • 2
  • 7
  • 2
    From what I know you will need a font that can display those characters. Nothing to do with the character encoding. – Luke Melaia Oct 27 '15 at 10:42
  • My GUI uses the default font which is "Lucida Grande" for me, which should support those characters. – Moritz Oct 27 '15 at 11:06
  • Check the font file and see if it has them, if not create them or find a font that includes them. Also if one of the answer(s) provided solves your problem; please mark it as the accepted answer. – Luke Melaia Oct 27 '15 at 16:11
  • Yeah sorry I was stupid. I thought changing to my editor font, where the emojis are displayed, would be enough. Thats not the case :). The `font.canDisplay()` method opened my eyes. I will include my own font to have an operating system independent solution. Still a strange behaviour that the "L" is also broken not only the emoji. – Moritz Oct 27 '15 at 19:23

1 Answers1

2
  • When using tree2.setFont(new Font("DejaVu Sans", 0, 16));, your code works fine.
    • I am running JDK1.8.0_66 on Windows 10 x64.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;

public class Demo2 {
  public JComponent makeUI() {
    //String s = "";
    String s = "\uD83D\uDE01"; //\u1F601
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode leaf = new DefaultMutableTreeNode("Leaf: " + s);
    root.add(leaf);

    JTree tree1 = new JTree(root);
    tree1.setFont(new Font("Lucida Sans", 0, 16));

    JTree tree2 = new JTree(root);
    tree2.setFont(new Font("DejaVu Sans", 0, 16));

    JPanel p = new JPanel(new GridLayout(2, 1));
    p.add(new JScrollPane(tree1));
    p.add(new JScrollPane(tree2));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new Demo2().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
  • `tree1` fixes the "L" for me but not the emoji. `tree2` gives me the same as the default font. So I assume the font is actually the problem and not that JTree doesn't support UTF-8. I'm on OSX 10.11.1 with JDK 1.8.0_51. Thanks for your example! – Moritz Oct 27 '15 at 15:18