2016-09-16 5 views
-1

最近、スタイル定数、スタイル付きドキュメント、テキストペインを使用しています。ユーザーのハイライト/ただ、「Hello World」の中に「Oのワール」を選択した場合、私は、一例としてスタイル付きドキュメント/テキストペインからフォント文字と色を取得

StyledDocument doc = this.tpText.getStyledDocument(); 
    Style style = this.tpText.addStyle("stylish", null); 
    StyleConstants.setForeground(style, color.BLACK); 
    StyleConstants.setFontFamily(style, "Arial"); 
    doc.setCharacterAttributes(this.tpText.getSelectionStart(), this.tpText.getSelectionEnd() - this.tpText.getSelectionStart(), this.tpText.getStyle("stylish"), true);//This is the piece of code (last line) that will set all the attributes to the highlited text. 

以下のコードでそれを示す選択あなたが/ハイライトテキストの一部を編集することができます知っています"o worl"は黒色に変更され、フォントレターはArialになります。

私の質問は、どのように高級/選択されたテキストからフォント文字と色を得ることができるかということです。私はそれを別々の変数に保存する方法を知りたい(1つはカラー用、もう1つはフォントレター用)。

答えて

0

[OK]を、私は答えを見つけたより多くの研究をした後。

doc = this.tpText.getStyledDocument(); 
    Element element = doc.getCharacterElement(this.tpText.getSelectionStart()); 
    AttributeSet as = element.getAttributes(); 
    colour = StyleConstants.getForeground(as); 

私がこのスタイル定数メソッドについて見ている唯一の欠点は、最初の文字属性を認識し、他の文字属性を上書き/無視することだけです。多分ループで私はそれをすることができます。

関連する問題