Home Ask Login Register

Developers Planet

Your answer is one click away!

Siddharth February 2016

how to print a glyph of supplementary characters in java onto my JTextField when i just click the button

I have a simple program just need to set the character whose Unicode value larger the character data type (supplementary character) on JTextField when the button is click .Tell me i am really fed up and how i will do it .This problem have already taken my 4 days.

 //importing the packages
import java.awt.event.*;
import javax.swing.*;

import java.util.*;
import java.awt.*;

//My own custom class 
public class UnicodeTest implements ActionListener
JFrame jf;
JLabel jl;
JTextField jtf;
JButton jb;
    jf=new JFrame();// making a frame
    jf.setLayout(null); //seting the layout null of this frame container
    jl=new JLabel("enter text"); //making the label 
    jtf=new JTextField();// making a textfied onto which a character will be shown
    jb=new JButton("enter");
//setting the bounds 
    jb.setBounds(50, 230, 100, 100);
    jf.setVisible(true); //making frame visible
    jb.addActionListener(this); //  registering the listener object
public void actionPerformed(ActionEvent e) // event generated on the button click
{   try{

           int x=66560; //to print the character of this code point

           jtf.setText(""+(char)x);// i have to set the textfiled with a code point character which is supplementary in this case 

     catch(Exception ee)// caughting the exception if arrived
        { ee.printStackTrace(); // it will trace the stack frame where exception arrive 
   // making the main method the starting point of our program
  public static void main(String[] args)

    //creating and showing this application's GUI.
      new UnicodeTest();     


user1803551 February 2016

Since you are not giving enough information on what's wrong, I can only guess that either or both:

  1. You are not using a font that can display the character.
  2. You are not giving the text field the correct string representation of the text.

Setting a font that can display the character

Not all fonts can display all characters. You have to find one (or more) that can and set the Swing component to use that font. The fonts available to you are system dependent, so what works for you might not work for others. You can bundle fonts when you deploy your application to ensure it works for everyone.

To find a font on your system that can display your character, I used

Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font f : fonts) {
    if (f.canDisplay(66560)) {

The output (for me) is a single font, so I allowed myself to set it in the loop:

java.awt.Font[family=Segoe UI Symbol,name=Segoe UI Symbol,style=plain,size=1]

as also noted in the comments to the question by Andrew Thompson.

Giving the text field the correct string representation

The text fields require UTF-16. Supplementary characters in UTF-16 are encoded in two code units (2 of these: \u12CD). Assuming you start from a codepoint, you can convert it to characters and then make a string from them:

int x = 66560;
char[] chars = Character.toChars(x); // chars [0] is \uD801 and chars[1] is \uDC00
textField.setText(new String(chars)); // The string is "\uD801\uDC00"
// or just
textField.setText(new String(Character.toChars(x)));

as notes by Andrew Thompson in the comments to this answer (previously I used a StringBuilder).

Post Status

Asked in February 2016
Viewed 3,549 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life