Unicode characters in app doesn't show correctly

java swing netbeans unicode fonts

790 просмотра

3 ответа

178 Репутация автора

How to convert string like that for example "Bohinjska Češnjica". The string that I get from the website and it is not encoded in unicode. It works just fine in program that uses swing(Netbeans), but when I past the link to jar to windows console(to run jframe application) it doesn't show correct characters in the string "Bohinjska Češnjica" for characters 'Č' and 'š'.

String example="Bohinjska Češnjica";

I get that string from website. How do I encode or show as it is ("Bohinjska Češnjica") in a Swing application, so when i will run a jframe application it will show me this characters(and others unicode characters ofcourse ('Ž','ž','č' and 'Š'))?

Link 1 :jar file of my program runned from console Link 2:when i run program from netbeans

READING CONTENT FROM WEBSITE :

             URL nov = new URL("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_avt.html");
            URLConnection conn = nov.openConnection(); //connect to a   website
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder niz = new StringBuilder(); 
            while ((inputLine = br.readLine()) != null) {
                String vrstica = inputLine.trim(); //reading html...

}

Автор: slodeveloper Источник Размещён: 13.03.2015 08:47

Ответы (3)


0 плюса

705 Репутация автора

Try using fonts which support unicode characters like Arial Unicode MS.

Автор: Mandeep Rajpal Размещён: 13.03.2015 09:03

5 плюса

155082 Репутация автора

At run-time, we can use Font.canDisplayUpTo(String) to determine which of the installed fonts can display a given text. Logical fonts such as Font.SANS_SERIF and Font.SERIF typically are made of of other fonts and can cover vast ranges of different scripts.

Here is an example using the given text, with the results seen on this machine.

enter image description here

BTW - Google translate is telling me that is Slovenian rather than Croation, but fortunately, the exact same technique will work for any script.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;

public class CroationTextInGUI {

    private JComponent ui = null;
    private String text = "Bohinjska Češnjica";

    CroationTextInGUI() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        String[] fontFamilies = GraphicsEnvironment.
                getLocalGraphicsEnvironment().
                getAvailableFontFamilyNames();
        Vector<String> croatFreindlyFonts = new Vector<String>();
        for (String name : fontFamilies) {
            Font font = new Font(name, Font.PLAIN, 20);
            if (font.canDisplayUpTo(text)<0) {
                croatFreindlyFonts.add(name);
            }
        }
        final JList list = new JList(croatFreindlyFonts);
        list.setVisibleRowCount(20);
        list.getSelectionModel().setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
        ui.add(new JScrollPane(list), BorderLayout.LINE_START);

        final JTextArea output = new JTextArea(text, 2, 12);
        output.setLineWrap(true);
        output.setWrapStyleWord(true);
        ui.add(new JScrollPane(output));

        ListSelectionListener showFontListener = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                Font f = new Font(
                        list.getSelectedValue().toString(), Font.PLAIN, 50);
                output.setFont(f);
            }
        };
        list.addListSelectionListener(showFontListener);
        list.setSelectedIndex(0);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CroationTextInGUI o = new CroationTextInGUI();

                JFrame f = new JFrame("Croation Text in GUI");
                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);
    }
}
Автор: Andrew Thompson Размещён: 13.03.2015 09:40

0 плюса

178 Репутация автора

Решение

BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));

Автор: slodeveloper Размещён: 14.03.2015 10:29
Вопросы из категории :
32x32