微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

删除 JTextPane 行并保持样式

如何解决删除 JTextPane 行并保持样式

我使用 JTextPane 和 StyledDocument 来设置消息样式,我想清除消息或仅清除最旧的消息。 我可以使用以下方法轻松清除所有内容

textPane.setText("");

但是如果我想清除所有内容,除了某些行,则不确定是否/如何完成。 我试过了

textPane.setText(textPane.getText().substring(0,Math.min(200,textPane.getText().length())));

但问题是它删除内容样式。

这是显示问题的简单演示。 有什么简单的方法吗?

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,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);
    }
}

解决方法

了解如何使用 Element 进行操作。例如,下面的代码将只保留最新的 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();
                }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?