0

I'm having difficulties with my logger that I embedded into a JTextArea: when I call the logger.append() method, it just works fine, but when I use System.out.println(), all my French accents are lost.

Here, you have the creation of the logger and the redirection of System.out and System.err to it.

this.logger = new TextAreaOutputStream(jta, jsp.getVerticalScrollBar());
System.setProperty("user.langage", "fr");
PrintStream ps;
ps = new PrintStream(this.logger, true, "UTF-8");
System.setOut(ps);
System.setErr(ps);

Here, you have the TextAreaOutputStream class.

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;

public class TextAreaOutputStream extends OutputStream {
    private JTextArea jta;
    private JScrollBar jsb;

    public TextAreaOutputStream(JTextArea jta, JScrollBar jsb) {
        this.jta = jta;
        this.jsb = jsb;
    }

    public synchronized void append(String s) {
        this.jta.append(s);
        scroll();
    }

    public synchronized void write(int b) throws IOException {
        jta.append(String.valueOf((char) b));
        if (((char) b) == '\n')
            scroll();
    }

    private synchronized void scroll() {
        if ((jsb.getValue() + jsb.getVisibleAmount()) == jsb.getMaximum()) 
            jta.setCaretPosition(jta.getDocument().getLength());
    }
}

I tried to change the encoding of the PrintStream to random encodings, it changed the look of the misprinted accents, but I never could have it right. I also tried to change the accents with unicode entries such as \u00e9 for é, but it did not change anything.

Here, I'm desperate enough to ask for your help,

Romain

Maveric78f
  • 55
  • 6
  • 1
    Are you sure about "user.langage"? Isn't it "user.language" (note the 'u')? – fge Jun 24 '13 at 10:03
  • 1
    You should check what encoding are you using in your development tool. If you use Eclipse click left on the Project -> Properties -> Resource (and check you are using UTF-8 encoding) – maqjav Jun 24 '13 at 10:04
  • @fge It's clearly wrong, but then again, it has probably nothing to do with the issue. – Marko Topolnik Jun 24 '13 at 10:10
  • maqjav > I checked the place you mentioned but `UTF-8` was already my default-inherited encoding. The encoding error must come from another issue. @fge and @Marko > you're both right. It's wrong but irrelevant to the bug. Got it corrected anyway, so thanks ;-) – Maveric78f Jun 24 '13 at 10:13

1 Answers1

1

Your implementation of OutputStream is wrong: you pretend that write(int) receives characters, but in fact it receives raw bytes. If you check out PrintStream, you'll see that its print and append methods first apply encoding and then eventually call write(int) for each encoded byte.

You shoud not attempt to transfer raw bytes back to the string in your TextArea. Instead implement PrintStream and its character-based methods and shove those strings/char arrays directly into the UI component.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I'm not sure to understand your advice. My TextAreaOutputStream implementation has been inspired by many implementation examples of such a class I found on the web (including SO). Those are all `TextArea` extending an `OutputStream`. Also the `print` methods of `PrintOutput` are all 1-liners using `String.valueOf()` methods in the same way I do. – Maveric78f Jun 24 '13 at 12:31
  • I can only say that those examples are ugly hacks, and you are the one who exposes them as such: they work only for ASCII or other single-byte encondings. – Marko Topolnik Jun 24 '13 at 12:43