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:
- Root
|-- []eaf: []
Is it possible to display those strings correctly using a JTree?
For future readers:
Maybe you want to check this, too. Click