2

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • https://github.com/vdurmont/emoji-java/issues/131 – Madhawa Priyashantha May 18 '21 at 02:37
  • 2
    Don't know what `EmojiParser` does, but if I change the code to `String result = "With \u2764 Nicoll";`, I get a heart when running on Windows 10 with Java 15. See [picture](https://i.stack.imgur.com/RIeJR.png). --- Maybe it's using a font that doesn't support emoji's. – Andreas May 18 '21 at 02:51
  • I use ubuntu, dont work – Nicoll Mejia May 18 '21 at 03:00
  • What other font can I use? Thanks! – Nicoll Mejia May 18 '21 at 03:08
  • Any font that supports unicode characters should work. I don't know what fonts are available in Ubuntu, but you could try a generic Sans Serif font supported by the JVM, for example `yourLabel.setFont(new Font("SansSerif", Font.PLAIN, fontSize));` – sorifiend May 18 '21 at 03:24

1 Answers1

2

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;
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433