2009-10-29 4 views
4

Java Swing에서 단락의 배경색을 변경할 수 있습니까? setParagraphAttributes 메서드 (아래 코드)를 사용하여 설정하려고했지만 작동하지 않는 것 같습니다.JTextPane에서 단락의 배경색 변경 (Java Swing)

StyledDocument doc = textPanel.getStyledDocument(); 
    Style style = textPanel.addStyle("Hightlight background", null); 
    StyleConstants.setBackground(style, Color.red); 

    Style logicalStyle = textPanel.getLogicalStyle(); 
    doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle("Hightlight background"), true); 
    textPanel.setLogicalStyle(logicalStyle); 
+0

특정 배경색으로 단락 요소 속성을 (올바르게) 설정하면 해당 단락의 문자에만 영향을줍니다. 단락의 오른쪽 (또는 왼쪽)에 영향을주지 않습니다. 그러나, 커스터마이징 된'Highlighter.HighlightPainter'는'JTextComponent'의'Highlighter'에 제공 될 수 있습니다. –

답변

3

은 UPDATE : 나는 방금 전화 Highlighter.I 당신이 setBackground의 스타일을 사용해야한다고 생각 해달라고 클래스에 대해 알게되었습니다. DefaultHighlighter 클래스를 대신 사용하십시오.

Highlighter h = textPanel.getHighlighter(); 
h.addHighlight(1, 10, new DefaultHighlighter.DefaultHighlightPainter(
      Color.red)); 

addHighlight 메서드의 처음 두 매개 변수는 강조 표시 할 텍스트의 시작 인덱스와 끝 인덱스 일뿐입니다. 이 메서드를 여러 번 호출하여 텍스트의 불연속 줄을 강조 표시 할 수 있습니다.

OLD 답변 :

나는 것 나던 setParagraphAttributes 방법이 작동하는 이유를 모르겠어요. 하지만이 일을하는 것 같습니다.

doc.insertString(0, "Hello World", textPanel.getStyle("Hightlight background")); 

어쩌면 당신은 ... 지금이 문제를 해결

+0

답장을 보내 주셔서 감사합니다. 위의 코드는 작동하지만 텍스트가있는 경우 배경색 만 변경합니다. 텍스트가없는 경우에도 배경색을 변경하고 싶습니다. (CSS의 배경색 속성과 동일) – Sudar

+0

CSS의 배경색을 변경하는 태그를 지정합니다. 당신은 jtextpane에서 무엇을 할 것입니까? 문제는 단락을 구분하고 색상을 설정하지 않아야한다는 것입니다. 원하는 경우 문자 (또는 미리 지정된 픽셀 영역) 또는 전체 창을 지정할 수 있습니다. JEditorPane을 사용하면 CSS가 JEditorPane에서 작동한다고 생각합니다 ... – Jaskirat

+0

BTW에서 CSS를 시도해 보았지만 CSS에서는 텍스트를 포함하지 않고 bgcolor를 사용할 수 없습니다. {: RGB (255,0,255) 배경 색상;}

이것은 정확히 무슨 뜻인지 몰라 ... 나는이 ' <스타일 유형 = "텍스트/CSS"> P 시도 단락. 그래서 그것의 강조 표시되지 않은 텍스트를 포함 나던 파라 아래

그 ...

' – Jaskirat

3

를 해킹 작업 할 수 있습니다 내가 사용 :

SimpleAttributeSet background = new SimpleAttributeSet(); 
StyleConstants.setBackground(background, Color.RED); 

그런 다음 당신이 사용하는 기존의 속성을 변경할 수 있습니다

doc.setParagraphAttributes(0, doc.getLength(), background, false); 

또는 텍스트가있는 속성 추가 :

doc.insertString(doc.getLength(), "\nEnd of text", background); 
+0

나는 전체 텍스트 창 색깔 싶지 않아.한 단락 만 색칠 만하면됩니다. 나는 당신의 접근 방식을 시도했으나 효과가없는 것 같습니다. – Sudar

+0

물론 있습니다. 시작, 길이 값을 변경하기 만하면됩니다. 각 메소드의 매개 변수가 작동하는 방식을 이해하기 위해 API를 읽습니다. – camickr

0

선택한 텍스트 나 단락의 배경색을 쉽게 변경할 수 있습니다.

//choose color from JColorchooser 
    Color color = colorChooser.getColor(); 

    //starting position of selected Text 
    int start = textPane.getSelectedStart(); 

    // end position of the selected Text 
    int end = textPane.getSelectionEnd(); 

    // style document of text pane where we change the background of the text 
    StyledDocument style = textPane.getStyledDocument(); 

    // this old attribute set of selected Text; 
    AttributeSet oldSet = style.getCharacterElement(end-1).getAttributes(); 

    // style context for creating new attribute set. 
    StyleContext sc = StyleContext.getDefaultStyleContext(); 

    // new attribute set with new background color 
    AttributeSet s = sc.addAttribute(oldSet, StyleConstants.Background, color); 

// set the Attribute set in the selected text 
    style.setCharacterAttributes(start, end- start, s, true);