• category: swing folder: HTMLEditorKit title: JTextPaneで修飾したテキストをJTextAreaにHtmlソースとして表示する tags: [JTextPane, HTMLEditorKit, Html, JPopupMenu, JTextArea, JTabbedPane, ChangeListener] author: aterai pubdate: 2013-04-01T00:08:05+09:00 description: HTMLEditorKitを使用するJTextPaneで修飾したテキストをJTextAreaにHtmlソースとして表示、編集、JTextPaneに反映するテストを行なっています。 image: https://lh6.googleusercontent.com/-ORS7lITRAUE/UVhL_1G6hPI/AAAAAAAABo4/5WKtBFFthJ0/s800/HTMLEditorKit.png

概要

概要

HTMLEditorKitを使用するJTextPaneで修飾したテキストをJTextAreaHtmlソースとして表示、編集、JTextPaneに反映するテストを行なっています。

サンプルコード

サンプルコード

textPane.setComponentPopupMenu(new HTMLColorPopupMenu());
#spandel
//textPane.setEditorKit(new HTMLEditorKit());
#spanend
#spanadd
// textPane.setEditorKit(new HTMLEditorKit());
#spanend
textPane.setContentType("text/html");
textArea.setText(textPane.getText());

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("JTextPane", new JScrollPane(textPane));
tabbedPane.addTab("JTextArea", new JScrollPane(textArea));
tabbedPane.addChangeListener(new ChangeListener() {
  @Override public void stateChanged(ChangeEvent e) {
    JTabbedPane t = (JTabbedPane)e.getSource();
    JTabbedPane t = (JTabbedPane) e.getSource();
    int i = t.getSelectedIndex();
    try {
      if (i == 0) {
        textPane.setText(textArea.getText());
      } else {
        String str = textPane.getText();
        textArea.setText(str);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    t.revalidate();
  }
});
View in GitHub: Java, Kotlin

解説

HTMLEditorKitを使用(コンテンツ形式をtext/htmlに設定)するJTextPaneJEditorPane#getText()を実行すると、HTMLEditorKitからスタイル(文字色)などを設定したHtmlソースとして文字列を取得することができるので、これをJTabbedPaneJTextAreaに切り替わるときにJTextAreaに流しこんでいます。

解説

HTMLEditorKitを使用(コンテンツ形式をtext/htmlに設定)するJTextPaneJEditorPane#getText()を実行すると、HTMLEditorKitから文字色などのStyleを設定したHtmlソースとして文字列を取得可能なので、JTabbedPaneJTextAreaに切り替わるときにJTextAreaに流し込んでいます。 逆に、JTextAreaHtmlソースを編集し、JTabbedPaneJTextPaneに切り替える時には、JEditorPane#setText(String)内で、HTMLEditorKitHTML形式で読み込まれるようになっています。 逆に、JTextAreaHtmlソースを編集してJTabbedPaneJTextPaneに切り替える時には、JEditorPane#setText(String)内でHTMLEditorKitHTML形式で読み込まれるよう設定しています。
  • メモ
    • textPane.setContentType("text/html");とコンテンツ形式を設定しておかないと、JEditorPane#setText(String)Documentが更新されない場合がある?
    • この場合、以下のように、textPane.setText(textArea.getText());ではなく、HTMLEditorKit#insertHTML(...)を使用する
  • textPane.setContentType("text/html");とコンテンツ形式を設定しておかないとJEditorPane#setText(String)Documentが更新されない場合がある?
  • この場合以下のようにtextPane.setText(textArea.getText());ではなくHTMLEditorKit#insertHTML(...)を使用する
    #spandel
    //textPane.setText(textArea.getText());
    #spanend
    #spanadd
    // textPane.setText(textArea.getText());
    #spanend
    textPane.setText("");
    HTMLEditorKit hek = (HTMLEditorKit) textPane.getEditorKit();
    HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
    hek.insertHTML(doc, 0, textArea.getText(), 0, 0, null);
    

#spandel
//import java.io.StringReader;
#spanend
#spandel
//import javax.swing.text.html.parser.*;
#spanend
#spanadd
// import java.io.StringReader;
#spanend
#spanadd
// import javax.swing.text.html.parser.*;
#spanend
ParserDelegator delegator = new ParserDelegator();
#spandel
final StringBuffer s = new StringBuffer();
#spanend
#spanadd
StringBuffer s = new StringBuffer();
#spanend
delegator.parse(new StringReader(str), new HTMLEditorKit.ParserCallback() {
  @Override public void handleText(char[] text, int pos) {
    s.append(text);
  }
}, Boolean.TRUE);
System.out.println(s.toString());

コメント

参考リンク

コメント