Resetting attributes in a Document after inserting a String

后端 未结 1 1473
借酒劲吻你
借酒劲吻你 2021-01-25 01:53

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 te
相关标签:
1条回答
  • 2021-01-25 02:45

    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();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题