Ves
Ves

Reputation: 25

How to set UTF-8 encoding for a file which opens on a seperate JFrame on a JTextPane

I have a separate class with a JFrame, that opens when user clicks a button on my main frame. The frame has a JTextPane which displays the content. I open my file with the code written below. But in Slovenia we have certain letters which don't show correctly (ex. Grilled chicken = piščanec na žaru... where š,č and ž show like squares).

My question is, is it even possible to set the UTF-8 encoding for the text or a file, without changing font? Font has to stay default (using NetBeans).

public class EditFrame extends javax.swing.JFrame {

    public void readFile(File f) {
        try {
            textPane.read(new java.io.FileReader(f), null);
        } catch (IOException ex) {
            Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    ...
}

Upvotes: 1

Views: 1119

Answers (1)

Arnaud
Arnaud

Reputation: 17524

You have to specify the encoding of the file you are reading from.

It may be done like this :

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
textPane.read(reader, null);

Upvotes: 2

Related Questions