Results 1 to 12 of 12
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Unicode characters are not shown on the JTextPane

    Hi, I am trying to use JTextPane with unicode characters.

    Java Code:
    private void createPanelPerformanceText()
        {
            String myText = "SHOW UNICODE : \u2705 : " + String.valueOf("\u2705");
            
            // Creating a panel
            pnlPerformanceText = new JPanel();
            pnlPerformanceText.setLayout(new BorderLayout());
           
            // Creating pane
            panePerformanceText = new JTextPane();
            panePerformanceText.setContentType("text/html");
            
            SimpleAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setBold(set, true);
            panePerformanceText.setCharacterAttributes(set, true);
            StyleConstants.setFontSize(set, 35);
    
            Document doc = panePerformanceText.getStyledDocument();
            try {
                doc.insertString(doc.getLength(), myText, set);
            } catch (BadLocationException ex) {
                Logger.getLogger(View_MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            JScrollPane scrollPane = new JScrollPane(panePerformanceText);
            pnlPerformanceText.add(scrollPane,BorderLayout.CENTER);
        }
    However, I only see the first part of the string ("SHOW UNICODE : :") and the unicode characters are not there.

    Thanks a lot for your help!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Unicode characters are not shown on the JTextPane

    Please provide a complete, but short, compilable working example that demonstrates the problem. Should take less than 50 lines of code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Unicode characters are not shown on the JTextPane

    Does the font being used by the text panel actually have a glyph representing that code point?
    Looking it up it's a "white heavy check mark"...not something that would often appear in a standard font.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Unicode characters are not shown on the JTextPane

    Thanks, guys.

    Here is a simple program showing the problem. I am also trying to use the Arial font, which, as far as I understand, does support unicode characters:


    Java Code:
    package testunicode;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    
    
    public class TestUnicode 
    {
        private void createGUI()
        {
    
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Panel
            JPanel panel1 = new JPanel();
            panel1.setLayout(new java.awt.BorderLayout());
            panel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
            panel1.setPreferredSize(new Dimension(500,300));
            
            
            // JTextPane
            JTextPane panePerformanceText = new JTextPane();
            
            // Trying this and it does not work either
            //panePerformanceText.setContentType("text/html");
            
            SimpleAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setBold(set, true);
            StyleConstants.setFontSize(set, 14);
            StyleConstants.setFontFamily(set, "Arial");
            panePerformanceText.setCharacterAttributes(set, true);
            
            // Text
            String myText = "SHOW UNICODE : \u2705 : " + String.valueOf("\u2705");
            panePerformanceText.setText(myText);
    
            // Scroll Pane
            JScrollPane scrollPane = new JScrollPane(panePerformanceText);
            panel1.add(scrollPane,BorderLayout.CENTER);
            
            
            // Finalize frame
            frame.getContentPane().add(panel1, BorderLayout.CENTER);
    
            //4. Size the frame.
            frame.pack();
    
            //5. Show it.
            frame.setVisible(true);
        }
        
        
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(() -> 
            {
                TestUnicode application = new TestUnicode();
                application.createGUI();
            });
        }
        
    }
    Thank you!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Unicode characters are not shown on the JTextPane

    It may support unicode, but that doesn't mean it has a glyph for each one.
    Here, for example, is a list of the MS Arial unicode characters supported.
    Do a search for 'U+27' to take you to the dingbat block, and 2705 is missing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Unicode characters are not shown on the JTextPane

    Having said that, running the code you gave does show that tick.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Unicode characters are not shown on the JTextPane

    It does???!! I do not know why but I do not see it on my computer!

    Unicode characters are not shown on the JTextPane-unicode1.png

    I wonder why!

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Unicode characters are not shown on the JTextPane

    Same here, and I am on Linux and Gnome even :) What Java version are you running this on?

    Unicode characters are not shown on the JTextPane-screenshot-2017-09-26-14-28-01.png
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Unicode characters are not shown on the JTextPane

    It'll be the font.
    Something about your Arial font is missing that code point, I expect.

    What OS?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Unicode characters are not shown on the JTextPane

    Well, I ran the code and I see two empty squares (typical of an unsupported/unknown character). Windows 10 and latest Java 8 update and Eclipse Neon.2.

    UPDATE!!: Mea Culpa. I had updated to Java 8r144 but Eclipse was pointing to Java 8r102. Ugh. I re-ran it and got the desired result.
    102 on the left and 144 on the right.

    Unicode characters are not shown on the JTextPane-testrunimage.jpgUnicode characters are not shown on the JTextPane-testrunimage.jpg
    Regards,
    Jim
    Last edited by jim829; 09-26-2017 at 05:19 PM. Reason: s/in/on
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Unicode characters are not shown on the JTextPane

    Oh yes, that's a point.
    "Glyph not found" is usually that square image isn't it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Unicode characters are not shown on the JTextPane

    This is crazy but I made sure that I have updated my SDK to 1.8.144, made sure that Java Platform Manager in Netbeans shows idk 1.8.0_144, ran the code again and the same problem. ((((
    I have macOS Sierra 10.12.6 and Netbeans 8.1. Thank you, guys. You confirmed that it is not the problem with my code, but there is something wrong with the way the unicode is displayed on Mac. Does anyone similar system to test it on?
    Cheers!

    UPDATE:

    Tried it on Eclipse. Now, I see the characters in the console, but the application still misses them. Mac.......

    By the way, this works well in my xCode using Swift, so it must be a combination of Mac+Java...

    Unicode characters are not shown on the JTextPane-unicode1.png

    UPDATE:

    Yeah, definitely a problem with fonts being able to show certain glyphs. E.g., u2713 works just fine.

    Thank you so much, everyone, for your help!
    Last edited by igorland; 09-27-2017 at 03:54 AM.

Similar Threads

  1. Displaying Unicode Characters
    By whatif in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2011, 02:44 AM
  2. JTextPane Unicode/UTF-8 problem?
    By LeonLanford in forum AWT / Swing
    Replies: 15
    Last Post: 08-16-2010, 02:36 PM
  3. Problem with writing unicode characters in a file
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 10:47 PM
  4. Replies: 2
    Last Post: 02-15-2010, 06:49 AM
  5. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •