HTMLコンテンツを含むJTextPaneがあり、CSSルールを含むセットStyleSheetを追加します。CSS ルールが実行されるように、html 要素に JComponent を挿入したいと考えています。html は次のようになります。
<body>
<div id = 'content'>
<p class = 't'>t</p>
<p class = 'k'>k</p>
<div class = 'hascomp'>
<span class = 'desc'>description</span>
<br/>
// here I want to insert my java component
</div>
</div>
</body>
以下は、hascomp クラスで要素を作成するための私の Java コードです。
try {
doc.insertBeforeEnd(content, "<div class = 'hascomp'><span class = 'desc'>"+description+"</span><br/>");
} catch (BadLocationException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int offset = Pane.getDocument().getLength();
Pane.setCaretPosition(offset);
Pane.insertComponent(jcomponent);
try {
doc.insertBeforeEnd(content, "</div>");
} catch (BadLocationException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
hascomp クラスの CSS:
.hascomp {font : 10px monaco; background-color:#E15AE3;margin : 2px;padding : 1px 10px;border-radius: 4px;border-width: 1px;border-style: solid;border-color: #D5D3CD;text-align: left;clear: both;float: left; }
しかし、正しく動作しませんでした。
では、JTextPane の html 要素に JComponent を埋め込むにはどうすればよいでしょうか?!