2014-04-06 1 views
0

JTextPane으로 작업하고 있습니다.JTextPane 문서의 기본 속성은 어떻게 찾을 수 있습니까?

JTextPane pane = new JTextPane(); 
String content = "I'm a line of text that will be displayed in the JTextPane"; 
StyledDocument doc = pane.getStyledDocument(); 
SimpleAttributeSet aSet = new SimpleAttributeSet(); 

나는이 같은 textpane의 문서에이 aSet을 추가하는 경우 :

doc.setParagraphAttributes(0, content.length(), aSet, false); 

아무것도 볼 수 발생합니다. aSet에 대한 맞춤 속성을 설정하지 않았으므로 놀랄 일이 아닙니다. 그러나, 나는 aSet이 같은 doc의 현재 ParagraphAttributes을 대체 할 수있는 경우 :

doc.setParagraphAttributes(0, content.length(), aSet, true); 

많은 것들이 일어날. JTextPane 문서의 기본값에 대한 정보를 얻으려면 어떻게해야합니까? 특히 내 문제는 aSet에 대한 사용자 지정 글꼴을 정의 할 때 현재 특성을 바꿀 것으로 설정하면 글꼴이 굵은 글꼴로 표시됩니다. StyleConstants.setBold(aSet, false); 도움이되지 않습니다.

답변

3

source code을보고 어떤 정보 구조가 원하는 정보를 보유하고 있는지 확인했습니다. 이것은 각 단락의 속성을 인쇄하는 코드의 수정입니다. related SO question보고, 특정 문제에 대한

class javax.swing.text.StyleContext$NamedStyle 
NamedStyle:default {foreground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],size=12,italic=false,name=default,bold=false,FONT_ATTRIBUTE_KEY=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12],family=Dialog,} 

나는 문단의 텍스트를 설정 할 수 있었다 :

int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call 

Element section = doc.getDefaultRootElement(); 
int index0 = section.getElementIndex(offset); 
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0)); 
for (int i = index0; i <= index1; i++) 
{ 
    Element paragraph = section.getElement(i); 
    AttributeSet attributeSet = paragraph.getAttributes(); 
    Enumeration keys = attributeSet.getAttributeNames(); 
    while (keys.hasMoreElements()) 
    { 
     Object key = keys.nextElement(); 
     Object attribute = attributeSet.getAttribute(key); 
     //System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold" 
     System.out.println(attribute.getClass()); 
     System.out.println(attribute); 
    } 
} 

setText() 방법을 통해 추가 텍스트와 간단한 textPane의 출력을 제공합니다 변경할 수없는 aSet의 클래스가 javax.swing.text.StyleContext$SmallAttributeSet이 경우에

StyleContext sc = StyleContext.getDefaultStyleContext(); 
AttributeSet aSet = sc.addAttribute(aSet, StyleConstants.Bold, true); 

(IM하지 않습니다와 함께 굵게 plement MutableAttributeSet). 귀하의 경우에는 다음과 같이 표시됩니다.

aSet.addAttribute(StyleConstants.Bold, true); 

이 작동해야합니다.

+0

aSet의 속성을 설정하려면 'StyleConstants.setFontFamily (aSet, "Times New Roman");'을 사용하고 완벽하게 작동합니다. :) 당신의 대답은 내가 굵은 글씨로 생각한 것은 기본'[r = 51, g = 51, b = 51]'에서'[r = 0, g = 0, b = 0]으로 리셋하는 색상 속성 (전경) ]'. 매우 감사합니다. – user2651804

+0

@ user2651804 기꺼이 도와 드리겠습니다! 'StyleConstants.setXXX (aMutableSet, value)'메소드는 상품성을 위해 존재하며'aMutableSet.addAttribute (StyleConstants.XXX, value)'를 호출합니다. [예제] (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/StyleConstants.java#StyleConstants.setFontFamily%28javax.swing. text.MutableAttributeSet % 2Cjava.lang.String % 29). – DSquare

+0

우리의 삶을 편하게하기 위해 고안된 이러한 종류의 것들은 새로운 자바 프로그래머의 삶을 복잡하게 만듭니다. – user2651804

관련 문제