-1

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;
UnicodeTest()
{
    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 
    jl.setBounds(50,50,100,50);
    jtf.setBounds(50,120,400,100);
    jb.setBounds(50, 230, 100, 100);
    jf.add(jl);jf.add(jtf);jf.add(jb);
    jf.setSize(400,400);
    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
  • 12,965
  • 5
  • 47
  • 74
Siddharth
  • 117
  • 1
  • 10
  • 2
    *"Tell me i am really fed up.."* You are really fed up. `jf.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 08 '16 at 10:41
  • 1
    *"I have a simple program just need to.."* OK.. and what exactly is the problem? When I use that exact code I get illogical results. When I change the `setText` call to instead use the actual Unicode code point (as opposed to trying to cast an int to a char, which does not do what you expect) I get the 'missing glyph' character given the only font on this system that will render that code point is Segoe UI Symbol & the font of the text area is Arial (or some other sans serif font that does not support the character). – Andrew Thompson Feb 08 '16 at 10:54
  • 1
    Is the character supposed to be [this](http://www.fileformat.info/info/unicode/char/10400/index.htm)? – user1803551 Feb 08 '16 at 11:10
  • you are right but it doesn't lead me to the complete solution – Siddharth Feb 08 '16 at 11:50
  • / * what should i do now */ @AndrewThompson – Siddharth Feb 08 '16 at 11:52
  • 1
    The best strategy was summarized by @user1803551 more than 10 minutes before your latest comment. But my advice is: 1) Add a '?' to questions. 2) answer questions asked of you, like *"what exactly is the problem?"* 3) be alert to new solutions being offered, and if they don't work for you, explain why. – Andrew Thompson Feb 08 '16 at 12:00
  • i want a correct symbol/character display for the unicode code point onto my JTextField .@Andrew Thompson. I know there is font problem but exactly how to ractify that i really dont know man! – Siddharth Feb 08 '16 at 12:06
  • 1
    If you are a software engineer you surely have the capacity to ask a proper question, answer the questions in the comments and follow a given detailed answer. You are just repeating yourself in the comments. – user1803551 Feb 08 '16 at 12:13

1 Answers1

4

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)) {
        System.out.println(f);
        textField.setFont(f.deriveFont(20f));
    }
}

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).

enter image description here

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • 1
    @AndrewThompson Even better! Updated. – user1803551 Feb 08 '16 at 11:48
  • Thank You! You made my day dude! You Rock!!!!! @Andrew Thompson mistake was i didn't knew that the font has to be set on to that . Thank You soo much! – Siddharth Feb 08 '16 at 12:18
  • @AndrewThompson I think he meant "my mistake was that I didn't know...". – user1803551 Feb 08 '16 at 12:37
  • Suppose i have done both the step above mentioned then still correct character not displaying then what should be done ? @Andrew – Siddharth Feb 08 '16 at 12:43
  • 1
    *"then what should be done ?"* Post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) of your attempt. Add details about what you mean by 'correct character not displaying' (possibly along with a screenshot). Do all this on a new question thread. And don't forget to ask a specific question, and add a '?' to the end of it. – Andrew Thompson Feb 08 '16 at 13:40