swing - how to print a glyph of supplementary characters in java onto my JTextField when i just click the button -
i have simple program need set character unicode value larger character data type (supplementary character) on jtextfield when button click .tell me fed , how .this problem have taken 4 days.
//importing 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 frame jf.setlayout(null); //seting layout null of frame container jl=new jlabel("enter text"); //making label jtf=new jtextfield();// making textfied onto character shown jb=new jbutton("enter"); //setting 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 listener object } public void actionperformed(actionevent e) // event generated on button click { try{ int x=66560; //to print character of code point jtf.settext(""+(char)x);// have set textfiled code point character supplementary in case } catch(exception ee)// caughting exception if arrived { ee.printstacktrace(); // trace stack frame exception arrive } } // making main method starting point of our program public static void main(string[] args) { //creating , showing application's gui. new unicodetest(); } }
since not giving enough information on what's wrong, can guess either or both:
- you not using font can display character.
- you not giving text field correct string representation of text.
setting font can display character
not fonts can display characters. have find 1 (or more) can , set swing component use font. fonts available system dependent, works might not work others. can bundle fonts when deploy application ensure works everyone.
to find font on system can display character, used
font[] fonts = graphicsenvironment.getlocalgraphicsenvironment().getallfonts(); (font f : fonts) { if (f.candisplay(66560)) { system.out.println(f); textfield.setfont(f.derivefont(20f)); } }
the output (for me) single font, allowed myself set in loop:
java.awt.font[family=segoe ui symbol,name=segoe ui symbol,style=plain,size=1]
as noted in comments question andrew thompson.
giving text field correct string representation
the text fields require utf-16. supplementary characters in utf-16 encoded in 2 code units (2 of these: \u12cd
). assuming start codepoint, can convert characters , make string them:
int x = 66560; char[] chars = character.tochars(x); // chars [0] \ud801 , chars[1] \udc00 textfield.settext(new string(chars)); // string "\ud801\udc00" // or textfield.settext(new string(character.tochars(x)));
as notes andrew thompson in comments answer (previously used stringbuilder
).
Comments
Post a Comment