I have a JtextPane with formatted text. I need to copy the complete style and attributes from this text to transfer it to another JtextPane. Is there an example or code snippet
I've solved it.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class Main {
private JTextPane textPane1;
private JTextPane textPane2;
private Document doc1;
private Document doc2;
private JFrame frame1;
private JFrame frame2;
private MutableAttributeSet black;
private MutableAttributeSet red;
private AttributeSet attribute;
public Main() throws BadLocationException {
textPane1 = new JTextPane();
black = new SimpleAttributeSet();
red = new SimpleAttributeSet();
StyleConstants.setForeground(black, Color.black);
StyleConstants.setForeground(red, Color.red);
textPane1.setEditorKit(new StyledEditorKit());
doc1 = textPane1.getDocument();
append1("This is a Test!\n");
//set color = red
attribute = red;
append1("Hello world! Hello Stackoverflow\n");
//set color = black
attribute = black;
append1("the text is black again\n");
//IMPORTANT PART: attribute of each character from the styled text will
//be transfered to the second textpanel
StyledDocument styledDocument = textPane1.getStyledDocument();
Element element;
textPane2 = new JTextPane();
textPane2.setEditorKit(new StyledEditorKit());
doc2 = textPane2.getDocument();
for(int i=0; i