I am developing chat application in J2SE which can also send emoticons to other user.
Application uses https://github.com/vdurmont/emoji-java ( Vdurmont Emoji-java-2.1 jar),
I followed all thing whichever described on that link, All is running fine during development environment but when I make jar for same, when I am sending emoticons to other user on web then it showing code (Γ°ΕΈΛΒ‘ and ? ).
Firstly I think its an issue of loading files from folder so used ClassLoader, to get proper image but during creating jar it showing ?(question mark) , So I removed that code for understood you better.
Code is written below below:
public ChatUI() {
initComponents();
this.setLayout(new WrapLayout(FlowLayout.LEFT, 5, 5));
for (int i = 0; i < imageHexaCode.length; i++)
{
final javax.swing.JLabel imogis = new javax.swing.JLabel("&#x" + imageHexaCode[i] + ";");
imogis.setCursor(new Cursor(Cursor.HAND_CURSOR));
imogis.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[i] + ".png")));
imogis.setHorizontalTextPosition(JLabel.CENTER);
imogis.setVerticalTextPosition(JLabel.BOTTOM);
imogis.setFont(new Font(null, Font.PLAIN, 1));
imogis.setForeground(Color.white);
final int aa = i;
imogis.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel jl = new JLabel("&#x" + imageHexaCode[aa] + ";");
jl.setName("&#x" + imageHexaCode[aa] + ";");
jl.setFont(new Font(null, Font.PLAIN, 1));
jl.setHorizontalTextPosition(JLabel.CENTER);
jl.setVerticalTextPosition(JLabel.BOTTOM);
jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png")));
jl.setForeground(Color.white);
ChatPaneWrite.jtp.insertComponent(jl);
System.out.println("" + imogis.getText());
// you can open a new frame here as
// i have assumed you have declared "frame" as instance variable
}
});
this.add(imogis);
}
this.revalidate();
this.repaint();
}
Where imageHexaCode is array of string of imoticons.
static String[] imageHexaCode = {
"1f621",
"1f608",
"2764",
"1f494"
};
& jtp is JTextPane where inserting imoticons component when user click on label
ChatPaneWrite.jtp.insertComponent(jl);
Emoticons are stored in same package where I am writing that's why I did not use ClassLoader in line
jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png")));
or it can also write like for package ChatUI
jl.setIcon(new javax.swing.ImageIcon(getClass().ClassLoader.getResource("ChatUI/emoji_" + imageHexaCode[aa] + ".png")));
here is snap shots:
In jar at receive end showing that emoticons like this
Please help me to sort it out.
Very thankful to all in advance
Emojis look like images, or icons, but they are not. They are letters (characters) from the UTF-8 (Unicode) character set. UTF-8 covers almost all of the characters and symbols in the world.
emoji-java is a lightweight java library that helps you use Emojis in your java applications.
So, if you need to disable emoji in your text, convert the symbol to UTF-8 or UTF-16 sequence ( symbol. codePointAt(0).
If you mean e.g. π 'GRINNING FACE' (U+1F600), then write "π" if your source code is UTF-8, or "\uD83D\uDE00" if not.
emoji-java uses UTF-8 encoding for parsing and by default Netbeans also uses the same, that's why your parsing went fine when run in Netbeans.
But when you run the program as jar file, the encoding scheme will be based on your system environment.
As a quick solution, you can force use UTF-8 encoding to run your jar file.
java -Dfile.encoding=UTF-8 -jar your-jar-file.jar
Or if you want a programmatic solution, may use this code
System.setProperty("file.encoding", "UTF-8");
java.lang.reflect.Field charset = null;
charset = java.nio.charset.Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null, null);
It is a general issue in the emoji-java by vdurmont. Both the solution works for me.
The first step is to check that the image resources are actually there at runtime. For example, add some logging of the value of getClass().getResource("emoji_" + imageHexaCode[aa] + ".png")
to make sure it is not null
. It is possible that in your development environment these are available but due to the way you are building your production version they are somehow getting missed or put in the wrong directory.
However, if you don't mind, I would also like to suggest an alternative approach: rather than using labels and image icons, have you considered building your emoticons into a font and then shipping the font file with your application? This would have several significant advantages:
You wouldn't need any special handling for the emoticons: just put the unicode text into a component that is using your custom font and the emoticons as well as all the other text will just work.
The font images can be vector images which means that they can scale nicely as the users change font size/zoom level etc.
The user can cut/copy/paste text containing the emoticons; since it is text it will again 'just work'.
If you decide to take this route, there is an excellent open-source font editor that I have used before called FontForge: https://fontforge.github.io/en-US/ . It's pretty easy: you just design the image in the appropriate slot for the character code and then export the font file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With