Search code examples
javaswingunicodefontsemoji

How display Label with emojis using Swing JAVA


I try to display emojis using Java. I find https://github.com/vdurmont/emoji-java this librery, Its so helpfull but only work in terminal.

I try to show Label using swing

How I can display lable with emojis with swing JAVA?

package Graphic;

import com.vdurmont.emoji.EmojiParser;
import javax.swing.*;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class Graphic{

    public static void Graphic() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400); // Tamaño de la ventana principal
        frame.setTitle("Orders work");
        String withlove  = "With :heart: Nicoll";
        String result = EmojiParser.parseToUnicode(withlove);
        System.out.println(result);

        JLabel conamor    = new JLabel(result);

        conamor.setText(result);
        frame.add(conamor);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

enter image description here


Solution

  • What other font can I use?

    See this answer to get a list of installed fonts which will display all the characters of a String. This should be done at run-time, unless you are supplying a suitable Font with the app.

    Notes:

    1. The code will need to use the Unicode character which corresponds to the emoji.
    2. It will be monochrome, same color as the text. Like seen here.

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.util.*;
    
    public class IHeartNickel {
    
        private JComponent ui = null;
        Vector<String> fonts = new Vector<>();
        // heavy black heart in Unicode
        String heart = new String(Character.toChars(10084));
        String msg = "I " + heart + " Nickel (%1s)";
    
        IHeartNickel() {
            initUI();
        }
    
        public final void initUI() {
            if (ui != null) {
                return;
            }
    
            ui = new JPanel(new BorderLayout(4, 4));
            ui.setBorder(new EmptyBorder(4, 4, 4, 4));
            String[] allFonts = GraphicsEnvironment.
                    getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            for (String f : allFonts) {
                Font font = new Font(f, Font.PLAIN, 1);
                if (font.canDisplayUpTo(msg) < 0) {
                    fonts.add(f);
                }
            }
            JList list = new JList(fonts);
            list.setVisibleRowCount(10);
            list.setCellRenderer(new HeartListCellRenderer());
            ui.add(new JScrollPane(list));
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        public static void main(String[] args) {
            Runnable r = () -> {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                IHeartNickel o = new IHeartNickel();
                
                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                
                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());
                
                f.setVisible(true);
            };
            SwingUtilities.invokeLater(r);
        }
    
        class HeartListCellRenderer extends DefaultListCellRenderer {
    
            @Override
            public Component getListCellRendererComponent(
                    JList<? extends Object> list, 
                    Object value, 
                    int index, 
                    boolean isSelected, 
                    boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
                JLabel l = (JLabel)c;
                Font font = new Font(value.toString(), Font.PLAIN, 20);
                l.setText(String.format(msg, font.getFontName()));
                l.setFont(font);
                
                return l;
            }
        }
    }