Resetting attributes in a Document after inserting a String

不打扰是莪最后的温柔 提交于 2019-12-02 09:07:59

问题


I have a JTextComponent in which a user can enter text in two ways:

  • He can type text directly into it.
  • Using a second control, he can indirectly insert text into it. This is done by programmatically calling insertString().

The font used in the text inserted in the second way will be different than the font that is typed in directly. The font of the text typed in will be the default font of the JTextComponent.

Here is the code. The TODO is what I don't know how to do.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class ResetAttributesInDocument extends JApplet {

    private ButtonListener bl = new ButtonListener();
    private JTextPane myJTextComponent;

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

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

            SimpleAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setFontFamily(set, "Courier New");
            // Possibly add more attributes to set here.

            try {
                doc.insertString(caretPosition, "text in Courier New", set);
            } catch (BadLocationException e1) {
                e1.printStackTrace();
            }

            // TODO Reset the attributes back to what they originally were so
            // that any new text the user enters after the inserted text is in
            // the original font.
        }
    }
}

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


回答1:


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


来源:https://stackoverflow.com/questions/5955324/resetting-attributes-in-a-document-after-inserting-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!