0

I have made this simple program in which I would like to display the unicode rune character \u16e6 in a JTextArea. This symbol is displayed at the top of the JFrame, but not in the JTextArea. I have set the font of the JTextArea to be the same as the JFrame, but it will still only display \u16e6 as an empty box. How can I display this, or any arbitrary unicode character, in a JTextArea?

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


public class WTextArea{
   public static void main(String[] args){
      JFrame frame = new JFrame("\u16e6");
               frame.setSize(1000,1000);
               frame.setVisible(true);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      System.out.println(frame.getFont().toString());
      Container content = frame.getContentPane();

      JTextArea jta = new JTextArea();
      jta.setFont(frame.getFont());
      content.add(jta);

      jta.setText("\u16e6");

   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sean Letendre
  • 2,623
  • 3
  • 14
  • 32
  • What environment are you running in (Windows?) and what's the JVM's character encoding? Try [adding `-Dfile.encoding=UTF-8`](http://stackoverflow.com/q/361975/113632) to your `java` command, see if that helps. – dimo414 Apr 14 '16 at 18:21
  • `I have set the font of the JTextArea to be the same as the JFrame,` - The title bar of a JFrame is not a Swing component, it is an OS widget. So the font used by the OS is not the same as the Font returned in the getFont() method of the frame. I know this doesn't solve the problem, it just explains that the Fonts are different, which is why the text area can't render the character. So you need to find a font that renders the "\u16e6" character. I don't know how to do that. – camickr Apr 14 '16 at 19:31
  • @AndrewThompson why is this a duplicate of a closed question? It seems like a perfectly reasonable thing to ask. – dimo414 Apr 14 '16 at 22:47
  • @dimo414 *"why is this a duplicate of a closed question?.."* Because the first answer gives a complete example of how to find suitable fonts. *"It seems like a perfectly reasonable thing to ask."* Close votes for lack of MCVE or duplicate do not imply unreasonable. – Andrew Thompson Apr 14 '16 at 23:00
  • @AndrewThompson, an MCVE was posted. Regarding the duplicate, I also did a search before posting my answer but did not find your answer. But you are right, your solution is much more complete than mine. All you need to do is change the text string to include the unicode character you want to display. At least with my brute force method I came up with the same answer :) – camickr Apr 15 '16 at 00:46

1 Answers1

0

I have set the font of the JTextArea to be the same as the JFrame, -

The title bar of a JFrame is not a Swing component, it is an OS widget. So the font used by the OS is not the same as the Font returned in the getFont() method of the frame. Therefore the Font of the text area is not the Font you think it should be which is why the text area can't render the character.

I have no ideas how to determine what the Font used by the OS frame is. Even if we could determine that, it may not be available to Swing.

So you need to find a font that renders the "\u16e6" character. The following program is a brute force method to find such a Font. It displays all the Fonts available to Swing. So I simply selected on Font at a time until I found a Font that display your symbol.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxFonts extends JFrame implements ItemListener
{
    JTextArea textArea;
    JComboBox comboBox;

    public ComboBoxFonts()
    {
        Font font = new Font("Courier New", Font.PLAIN, 16);

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
        Font [] fonts = ge.getAllFonts ();

        comboBox = new JComboBox( fonts );
        comboBox.setFont( font);
        comboBox.addItemListener( this );
        add( comboBox, BorderLayout.SOUTH );

        textArea= new JTextArea("Some text - \u16e6 -", 3, 20);
        textArea.setFont( font.deriveFont( 24.0f) );
        add( new JScrollPane( textArea ) );
    }

    public void itemStateChanged(ItemEvent e)
    {
        Font font = (Font)e.getItem();
        textArea.setFont( font.deriveFont( 24.0f ) );
    }

    public static void main(String[] args)
    {
        ComboBoxFonts frame = new ComboBoxFonts();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}

On my Windows platform the only Font appears to be:

//jta.setFont(frame.getFont());
jta.setFont(new Font("Segoe UI Symbol", Font.PLAIN, 18) );
camickr
  • 321,443
  • 19
  • 166
  • 288