Swing/StyleConstants のバックアップ差分(No.24)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/StyleConstants へ行く。
- 1 (2004-03-11 (木) 15:48:28)
- 2 (2004-03-12 (金) 01:57:07)
- 3 (2004-06-02 (水) 10:00:46)
- 4 (2004-08-31 (火) 12:26:50)
- 5 (2004-10-08 (金) 06:25:42)
- 6 (2004-10-19 (火) 08:00:32)
- 7 (2004-11-04 (木) 10:11:55)
- 8 (2005-02-03 (木) 02:04:23)
- 9 (2005-04-28 (木) 04:33:01)
- 10 (2005-05-11 (水) 23:54:11)
- 11 (2005-10-20 (木) 14:43:37)
- 12 (2006-02-27 (月) 16:30:07)
- 13 (2006-11-10 (金) 03:19:46)
- 14 (2007-04-11 (水) 16:05:29)
- 15 (2010-12-06 (月) 22:19:02)
- 16 (2013-02-20 (水) 15:38:32)
- 17 (2013-04-02 (火) 19:10:42)
- 18 (2014-12-25 (木) 16:09:52)
- 19 (2016-03-27 (日) 19:53:15)
- 20 (2017-04-04 (火) 14:17:08)
- 21 (2017-07-27 (木) 14:56:22)
- 22 (2018-07-27 (金) 16:27:13)
- 23 (2020-07-31 (金) 19:59:47)
- 24 (2021-12-24 (金) 12:38:13)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: StyleConstants title: JTextPaneに修飾した文字列を挿入 tags: [JTextPane, StyledDocument] author: aterai pubdate: 2004-01-12 description: JTextPaneに、スタイル付けした文字列を挿入して、ログ風に表示します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTT31r9lEI/AAAAAAAAAlI/7PqL2Aa3UJU/s800/StyleConstants.png --- * 概要 [#summary] `JTextPane`に、スタイル付けした文字列を挿入して、ログ風に表示します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTT31r9lEI/AAAAAAAAAlI/7PqL2Aa3UJU/s800/StyleConstants.png) * サンプルコード [#sourcecode] #code(link){{ StyledDocument doc = jtp.getStyledDocument(); Style def = doc.getStyle(StyleContext.DEFAULT_STYLE); Style error = doc.addStyle("error", def); StyleConstants.setForeground(error, Color.RED); // ... private void append(String str, boolean flg) { String style = flg ? StyleContext.DEFAULT_STYLE : "error"; StyledDocument doc = jtp.getStyledDocument(); try { doc.insertString(doc.getLength(), str + "\n", doc.getStyle(style)); } catch (BadLocationException e) { e.printStackTrace(); } } }} * 解説 [#explanation] 上記のサンプルでは、以下の手順で`JTextPane`にスタイルを設定した文字列を追加しています。 - `JTextPane`から`StyledDocument`を取得 - 予め設定しておいたエラー表示用の文字属性スタイルを`StyledDocument#getStyle("error")`で取得 - 予め設定しておいたエラー表示用の文字属性スタイルを`StyledDocument#getStyle("error")`メソッドで取得 - このスタイルと文字列と合わせて`Document#insertString(...)`メソッドで挿入 * 参考リンク [#reference] - [https://docs.oracle.com/javase/tutorial/uiswing/components/text.html Using Text Components (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] * コメント [#comment] #comment - 一々、`SimpleAttributeSet`を生成していたのを修正。 -- &user(aterai); &new{2010-12-06 (月) 22:24:36}; #comment