Java 删除JTextPane行并保留样式
我正在使用JTextPane和StyledDocument设置消息的样式,我希望清除消息或只清除最旧的消息。 我可以使用以下工具轻松清除所有内容:Java 删除JTextPane行并保留样式,java,jtextpane,styleddocument,Java,Jtextpane,Styleddocument,我正在使用JTextPane和StyledDocument设置消息的样式,我希望清除消息或只清除最旧的消息。 我可以使用以下工具轻松清除所有内容: textPane.setText(""); 但是,如果我想清除除某些行以外的所有行,则不确定是否/如何可以完成。 我试过了 但问题是它删除了内容样式 下面是一个简单的演示,展示了这个问题。 有没有简单的方法 import java.awt.Color; import java.awt.Dimension; import java
textPane.setText("");
但是,如果我想清除除某些行以外的所有行,则不确定是否/如何可以完成。
我试过了
但问题是它删除了内容样式
下面是一个简单的演示,展示了这个问题。
有没有简单的方法
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class TestJTextPane {
private JTextPane infoTextPane = new JTextPane();
private StyledDocument styledDocument;
private SimpleAttributeSet attributeSet = new SimpleAttributeSet();
private JButton addText;
private JButton clearText;
public static void main(String[] argv) {
new TestJTextPane();
}
public TestJTextPane(){
addText = new JButton("add");
addText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
StyleConstants.setForeground(attributeSet, Color.GREEN);
StyleConstants.setBackground(attributeSet, Color.BLACK);
StyleConstants.setBold(attributeSet, true);
StyleConstants.setFontSize(attributeSet, 20);
styledDocument.insertString(styledDocument.getLength(), "sample text message to add\n", attributeSet);
infoTextPane.setCaretPosition(infoTextPane.getDocument().getLength());
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
clearText = new JButton("clear");
clearText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//infoTextPane.setText("");
infoTextPane.setText(infoTextPane.getText().substring(0, Math.min(200, infoTextPane.getText().length())));
}
});
styledDocument = infoTextPane.getStyledDocument();
JPanel p = new JPanel();
p.add(addText);
p.add(clearText);
p.add(infoTextPane);
JFrame f = new JFrame("HyperlinkListener");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.setPreferredSize(new Dimension(400, 400));
f.pack();
f.setVisible(true);
}
}
图2显示了如何使用元素执行此操作。例如,下面的代码将只保留最新的2条消息
Element root = infoTextPane.getDocument().getDefaultRootElement();
try {
while(root.getElementCount() > 3){
Element first = root.getElement(0);
infoTextPane.getStyledDocument().remove(root.getStartOffset(), first.getEndOffset());
}
} catch (BadLocationException e1) {
// FIXME Auto-generated catch block
e1.printStackTrace();
}
intstartindex=0;int-endIndex=41;jTextPane1.select(开始索引,结束索引);jTextPane1.replaceSelection(“”)代码>。
Element root = infoTextPane.getDocument().getDefaultRootElement();
try {
while(root.getElementCount() > 3){
Element first = root.getElement(0);
infoTextPane.getStyledDocument().remove(root.getStartOffset(), first.getEndOffset());
}
} catch (BadLocationException e1) {
// FIXME Auto-generated catch block
e1.printStackTrace();
}