Resetting attributes in a Document after inserting a String

霸气de小男生 提交于 2019-12-02 07:26:48

Your question doesn't make sense.

First you state "if the user types into ..." which implies the user does something.

Then you say you want "to do something like this...", which implies you are doing something in the code which is not under user control because you are manually invoking the insertString() method.

Post your SSCCE that demonstrates the problem and describe the exact steps to duplicate the problem.

Is there a way to reset the attributes back to what they originally were?

Attributes are reset every time the caret changes position so you need to handle this with a CaretListener. Something like:

// <applet code="ResetAttributesInDocument.class" width="400" height="400"></applet>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class ResetAttributesInDocument extends JApplet implements CaretListener
{
    private ButtonListener bl = new ButtonListener();
    private JTextPane myJTextComponent;
    private SimpleAttributeSet set;

    public void init() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        myJTextComponent = new JTextPane();
        myJTextComponent.addCaretListener( this );
        contentPanel.add(myJTextComponent, BorderLayout.CENTER);
        JButton insertTextButton = new JButton("Insert text");
        insertTextButton.addActionListener(bl);
        contentPanel.add(insertTextButton, BorderLayout.SOUTH);
        getContentPane().add(contentPanel);

        set = new SimpleAttributeSet();
        StyleConstants.setFontFamily(set, "Courier New");
        StyleConstants.setForeground(set, Color.GREEN);
    }

    public void caretUpdate(CaretEvent e)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                String styleFamily = StyleConstants.getFontFamily( set );

                AttributeSet attributes = myJTextComponent.getCharacterAttributes();
                String attributeFamily = attributes.getAttribute(StyleConstants.FontFamily).toString();

                if (! styleFamily.equals(attributeFamily))
                {
                    StyledEditorKit k = (StyledEditorKit)myJTextComponent.getEditorKit();
                    k.getInputAttributes().removeAttributes(set);
                }
            }
        });
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            final Document doc = myJTextComponent.getDocument();
            final int caretPosition = myJTextComponent.getCaretPosition();

            try
            {
                doc.insertString(caretPosition, "text in Courier New", set);
            }
            catch (BadLocationException e1) {
                e1.printStackTrace();
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!