2010-10-20 33 views
6

Vì vậy, tôi đã tạo lớp khung văn bản của riêng mình (mở rộng JTextPane) và tôi đang sử dụng phương pháp bên dưới để thêm văn bản vào đó. Tuy nhiên, cửa sổ cần phải chỉnh sửa để thêm văn bản, nhưng điều này cho phép người dùng chỉnh sửa nội dung trong ngăn là tốt.Thêm văn bản vào JTextPane mà không cần người dùng chỉnh sửa?

Bất cứ ai có thể cho tôi biết cách thêm văn bản vào ngăn không cho phép người dùng thao tác những gì đang có?

public void appendColor(Color c, String s) { 
    StyleContext sc = StyleContext.getDefaultStyleContext(); 
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); 

    int len = getDocument().getLength(); 

    setCaretPosition(len); 

    setCharacterAttributes(aset, false); 

    replaceSelection(s); 

    setCaretPosition(getDocument().getLength()); 
} 

Trả lời

6

Cập nhật các tài liệu trực tiếp:

StyledDocument doc = textPane.getStyledDocument(); 
doc.insertString("text", doc.getLength(), attributes); 
3
JTextPane pane = new JTextPane(); 
pane.setEditable(false); // prevents the user from editting it. 
// programmatically put this text in the TextPane 
pane.setText("Hello you can't edit this!"); 
+0

Tôi hiểu rằng, nhưng làm thế nào tôi sẽ nối thêm văn bản vào cuối tài liệu? –

0

Ok Take 2:

JTextPane pane = new JTextPane(); 
pane.setEditable(true); 
DefaultStyledDocument document = (DefaultStyledDocument)pane.getDocument(); 
document.insertString("Hello you can't edit this!", document.getEndPosition().getOffset(), null); 
Các vấn đề liên quan