1

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

    ...
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ves
  • 25
  • 7
  • 1
    This question doesn't look related to swing at all, I would remove `swing`, `jframe` and `jtextpane` tags – Oneiros Apr 06 '17 at 13:27
  • *"seperate `JFrame`"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 06 '17 at 14:42
  • @Oneiros it concerns swing, since I am using it, and sometimes you can solve things without coding, just by clicking in the Design - properties window. – Ves Apr 07 '17 at 10:27
  • @AndrewThompson thanks for your comment, but I'm not wondering If I shoud use a seperate frame... I am going to use one for a better user experience in my case. – Ves Apr 07 '17 at 10:29
  • *"I am going to use one for a better user experience in my case."* My condolences to both your users. – Andrew Thompson Apr 07 '17 at 10:31

1 Answers1

2

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);
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 1
    Your answer helped me a lot, I changed the upper line into `BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "windows-1250"));` and it works perfectly :) – Ves Apr 07 '17 at 11:26