Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 232
Nicoll Mejia Avatar asked May 20 '26 19:05

Nicoll Mejia


1 Answers

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;
        }
    }
}
like image 79
Andrew Thompson Avatar answered May 24 '26 17:05

Andrew Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!