Swing/TextAreaOutputStream のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TextAreaOutputStream へ行く。
- 1 (2015-02-16 (月) 00:04:57)
- 2 (2015-02-25 (水) 17:19:56)
- 3 (2015-10-02 (金) 21:05:47)
- 4 (2016-05-30 (月) 17:59:51)
- 5 (2016-11-02 (水) 20:02:10)
- 6 (2017-11-18 (土) 13:55:06)
- 7 (2017-12-01 (金) 18:12:12)
- 8 (2018-02-24 (土) 19:51:30)
- 9 (2019-08-07 (水) 20:18:48)
- 10 (2019-11-27 (水) 13:51:45)
- 11 (2021-06-04 (金) 11:28:17)
- 12 (2022-08-05 (金) 16:01:40)
- 13 (2023-03-30 (木) 05:29:04)
--- title: JTextAreaにLoggerのログを出力する tags: [JTextArea, OutputStream, StreamHandler, Logger] author: aterai pubdate: 2015-02-16T00:00:51+09:00 description: Loggerのログ出力をJTextAreaに表示するためのOutputStreamとStreamHandlerを作成します。 --- * 概要 [#zcd68c56] `Logger`のログ出力を`JTextArea`に表示するための`OutputStream`と`StreamHandler`を作成します。 #download(https://lh3.googleusercontent.com/-SjJO0dTl1jg/VOChTiK0lPI/AAAAAAAANw4/elD2Gb4uBd0/s800/TextAreaOutputStream.png) * サンプルコード [#fe9ede5b] #code(link){{ class TextAreaOutputStream extends OutputStream { private final ByteArrayOutputStream buf = new ByteArrayOutputStream(); private final JTextArea textArea; public TextAreaOutputStream(JTextArea textArea) { super(); this.textArea = textArea; } @Override public void flush() throws IOException { super.flush(); buf.flush(); } @Override public void close() throws IOException { super.close(); buf.close(); } @Override public void write(int b) throws IOException { if (b == '\r') { return; } if (b == '\n') { final String text = buf.toString("UTF-8"); buf.reset(); EventQueue.invokeLater(new Runnable() { @Override public void run() { textArea.append(text + '\n'); textArea.setCaretPosition(textArea.getDocument().getLength()); } }); return; } buf.write(b); } } }} * 解説 [#if8895dc] - `TextAreaOutputStream` -- `ByteArrayOutputStream`を使用して、`JTextArea`に一行ずつ書き込みを行う -- `System.setOut(new PrintStream(new TextAreaOutputStream(textArea), true, "UTF-8"));`のように標準出力ストリームに割り当てると、`System.out.printlen("xxxxx")`などが`JTextArea`に出力されるようになる - `TextAreaHandler` -- ログ出力を上記の`TextAreaOutputStream`などに割り当てるため、`StreamHandler`を継承する`TextAreaHandler`を作成し、`Logger#addHandler(...)`で設定 -- `StreamHandler#setEncoding(...)`でエンコーディングを`UTF-8`に設定 -- `StreamHandler#publish(...)`、`StreamHandler#close(...)`をオーバーライド #code{{ class TextAreaHandler extends StreamHandler { private void configure() { setFormatter(new SimpleFormatter()); try { setEncoding("UTF-8"); } catch (IOException ex) { try { setEncoding(null); } catch (IOException ex2) { // doing a setEncoding with null should always work. // assert false; ex2.printStackTrace(); } } } public TextAreaHandler(OutputStream os) { super(); configure(); setOutputStream(os); } //@see java/util/logging/ConsoleHandler.java @Override public void publish(LogRecord record) { super.publish(record); flush(); } @Override public void close() { flush(); } } }} * 参考リンク [#ief84629] - [https://community.oracle.com/thread/1366824 System.out.println() Print to JFrame | Oracle Community] - [http://www.dreamincode.net/forums/topic/117537-external-program-output-to-jtextarea/ External Program Output To JTextArea - Java | Dream.In.Code] - [http://d.hatena.ne.jp/altcla/20091029/1256824750 標準出力を JTextArea に出力する - As I like it.] - [https://tips4java.wordpress.com/2008/11/08/message-console/ Message Console « Java Tips Weblog] * コメント [#t7095932] #comment #comment