Search code examples
javaunicodesymbolsmasking

How to mask HTML-Unicode in Java


How do I write a sign like ◄ and ► (funny... its not masked here by itself, here you go:) ◄ and ► in Java when I need this form \u00df (thats an "ß", fyi)... I tried just to put it in hex like \u25BA but that results in false symbols. What am I missing?

Please be so kind to post answer and method!

Thanks in advance!


Solution

  • This Swing code works for me:

    public static void main(String[] args) {
        String labelText = "\u25C4 \u25BA";
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        JLabel label = new JLabel();
        label.setText(labelText);
        panel.add(label);
        frame.pack();
        frame.setVisible(true);
    }
    

    Are you doing something very different?

    You can certainly mess it up by using fonts which do not support that character. So for example...

        JTextField field = new JTextField();
        field.setText(labelText);
    
        // this font has the symbol
        field.setFont(new Font("Courier New", Font.PLAIN, 12));
        panel.add(field);
        field = new JTextField();
        field.setText(labelText);
    
        // this font does not
        field.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 12));
        panel.add(field);
        panel.add(field);