Omphaloskopie
Omphaloskopie

Reputation: 2012

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!

Upvotes: 0

Views: 993

Answers (2)

ewan.chalmers
ewan.chalmers

Reputation: 16235

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);

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109532

If your page is in UTF-8:

response.setContentType("text/html; charset=UTF-8")

then you can leave the characters in the source. (Your IDE/editor has to be set to UTF-8 too.)

Upvotes: 0

Related Questions