Swing/HonorDisplayProperties のバックアップ(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/HonorDisplayProperties へ行く。
- 1 (2015-08-31 (月) 04:08:41)
- 2 (2015-08-31 (月) 15:00:29)
- 3 (2015-09-01 (火) 01:28:29)
- 4 (2017-04-04 (火) 14:13:45)
- 5 (2017-04-08 (土) 18:31:15)
- 6 (2018-03-27 (火) 15:12:45)
- 7 (2020-03-24 (火) 20:09:37)
- 8 (2021-09-30 (木) 09:46:41)
- 9 (2025-01-03 (金) 08:57:02)
- 10 (2025-01-03 (金) 09:01:23)
- 11 (2025-01-03 (金) 09:02:38)
- 12 (2025-01-03 (金) 09:03:21)
- 13 (2025-01-03 (金) 09:04:02)
- category: swing
folder: HonorDisplayProperties
title: JEditorPaneに設定したフォントをHTMLテキストに適用する
tags: [JEditorPane, HTMLEditorKit, Font, StyleSheet, HTML]
author: aterai
pubdate: 2015-08-31T03:59:20+09:00
description: HTMLEditorKitでbodyタグにデフォルトで指定されている文字サイズではなく、JEditorPaneに設定したフォントをHTMLテキストで使用します。
image:
概要
HTMLEditorKit
でbody
タグにデフォルトで指定されている文字サイズではなく、JEditorPane
に設定したフォントをHTML
テキストで使用します。
Screenshot

Advertisement
サンプルコード
editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
View in GitHub: Java, Kotlin解説
HTMLEditorKit
のデフォルトスタイルシートではbody
タグにfont-size: 14pt
などが設定されている- この設定が
HTML
テキストのデフォルト文字サイズになっているため、JEditorPane
にたとえばJEditorPane#setFont(new Font("Serif", Font.PLAIN, 16))
とフォントを変更しても反映されない
- この設定が
JEditorPane
に設定されたフォントを使用する場合は、JEditorPane#putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE)
としてコンポーネントのデフォルトのフォントを使用するように設定する必要がある
body
タグのスタイルを表示するサンプルコード- StyleSheet (Java Platform SE 8)のサンプル(
ShowStyles
)を参考StringBuilder buf = new StringBuilder(300); HTMLEditorKit htmlEditorKit = (HTMLEditorKit) editor.getEditorKit(); StyleSheet styles = htmlEditorKit.getStyleSheet(); // System.out.println(styles); Enumeration rules = styles.getStyleNames(); while (rules.hasMoreElements()) { String name = (String) rules.nextElement(); if ("body".equals(name)) { Style rule = styles.getStyle(name); Enumeration sets = rule.getAttributeNames(); while (sets.hasMoreElements()) { Object n = sets.nextElement(); buf.append(String.format("%s: %s<br />", n, rule.getAttribute(n))); } } } editor.setText(buf.toString());
- StyleSheet (Java Platform SE 8)のサンプル(