Zrobilem dwa JtextPane pierwszy słuzy do pisania mozna zmieniac styl
po nacisniecu przycisku drugi ma wyswielic ten string w takim stylu w jakim byl napisany
i własnie mam problem z wyswietlenie string w odpowiednim stylu

Robie to tak ze nasłuchuje pierwszy JTextPane CaretListener jesli jest zmiana stylu
dziele string dodaje do ArryList dodaje rownie styl do nastepnej ArryList

Podczas wyslania do drugiego JTextPane dodaje ten podzilony String wraz z odpowiednim
stylem i własnie tu jest problem bo ArryList z stylami zmienia wszystkie wartosc do
ostatniego stylu jaki bym przed wysłaniem np:

powinna byc

Styl[0] bold = true , italic =false
Styl[1] bold = false, italic =false
Styl[2] bold = true , italic =true

jest
Styl[0] bold = true , italic =true
Styl[1] bold = true , italic =true
Styl[2] bold = true , italic =true

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.*;

import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.*;
import javax.swing.text.rtf.RTFEditorKit;

public class przyk extends JFrame implements ItemListener,FocusListener,ActionListener,
CaretListener{

DefaultStyledDocument doc;
RTFEditorKit kit;
JToggleButton g,g2;
JTextPane textPane2,wyslij;
JButton b;
int start,end,stringStart = 0,stringEnd = 0,j=0;

String all,podziel;
boolean ok=true;
ArrayList listaString = new ArrayList();
//ArrayList listaStyl = new ArrayList();
Vector  listaStyl = new Vector();
MutableAttributeSet set,in;
MutableAttributeSet spr;
StyledDocument doc1,doc2;

public przyk(){
      super("TextComponentDemo");
    this.setLayout(null);

        doc = new DefaultStyledDocument();
        kit = new RTFEditorKit();

        textPane2 = new JTextPane();
        textPane2.setCaretPosition(0);

        JScrollPane paneScrollPane = new JScrollPane(textPane2);
        paneScrollPane.setBounds(10,10,300,300);
        paneScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        paneScrollPane.setPreferredSize(new Dimension(250, 155));
        paneScrollPane.setMinimumSize(new Dimension(10, 10));

        this.add(paneScrollPane );

        wyslij = new JTextPane();
        wyslij.setCaretPosition(0);
        wyslij.addCaretListener(this);

        JScrollPane paneScrollPane1 = new JScrollPane(wyslij);
        paneScrollPane1.setBounds(10,320,200,30);
        paneScrollPane1.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        //paneScrollPane1.set

        this.add(paneScrollPane1);
        /*textPane2.setEditorKit(kit);
        textPane2.setDocument(doc);
        textPane2.addFocusListener(this);*/

        wyslij.setEditorKit(kit);
        wyslij.setDocument(doc);
        wyslij.addFocusListener(this);

        g = new JToggleButton("B");
        g.setSize(60,30);
        g.setLocation(310,320);
        g.setActionCommand("bold");
        g.addItemListener(this);
        this.add(g);

        g2 = new JToggleButton("I");
        g2.setSize(60,30);
        g2.setLocation(380,320);
        g2.setActionCommand("italic");
        g2.addItemListener(this);
        this.add(g2);

        b = new JButton("IA");
        b.setSize(60,30);
        b.setLocation(230,320);
        b.setActionCommand("b");
        b.addActionListener(this);
        this.add(b);

        doc1 = wyslij.getStyledDocument();
        doc2 = textPane2.getStyledDocument();

        set = new SimpleAttributeSet( );
        StyleConstants.setItalic( set, false);
        StyleConstants.setBold( set, false);
        setAtt( set );
}

public void setAtt(AttributeSet attr)
{
      int st = wyslij.getSelectionStart( );
      int fn = wyslij.getSelectionEnd( );
      if ( !wyslij.hasFocus( ) )
         {
            st = start;
            fn = end;
         }
      if ( st != fn )
         {
            doc1.setCharacterAttributes( st, fn - st, attr, false );

         }
      else
         {
          in =  kit.getInputAttributes( );
            in.addAttributes( attr );

         }

}

public static void main(String[] args) {
    przyk p = new przyk();
    p.setSize(500,400);
    p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p.setVisible(true);

}

public void itemStateChanged(ItemEvent e) {
    g = (JToggleButton)e.getSource();
    g2 = (JToggleButton)e.getSource();
    String a = g.getActionCommand();
    String a2 = g2.getActionCommand();
    if( a.equals("bold") ){
        if( e.getStateChange() == 1){
            System.out.println("bold_jest_on");

               set = new SimpleAttributeSet( );
                StyleConstants.setBold( set, true);
                setAtt( set );
                wyslij.grabFocus( );

        }
        else if( e.getStateChange() == 2){
            System.out.println("bold_jest_off");
            set = new SimpleAttributeSet( );

            StyleConstants.setBold( set, false);
            setAtt( set );
            wyslij.grabFocus( );

        }
    }

    if( a2.equals("italic") ){
        if( e.getStateChange() == 1){
            System.out.println("italc_jest_on");

            set = new SimpleAttributeSet( );
            StyleConstants.setItalic( set, true);
            setAtt( set );
            wyslij.grabFocus( );

        }
        else if( e.getStateChange() == 2){
            System.out.println("ilatci_jest_off");
            set = new SimpleAttributeSet( );
            StyleConstants.setItalic( set, false);
            setAtt( set );
            wyslij.grabFocus( );

        }

    }

}

public void focusGained(FocusEvent arg0) {
    if ( start >= 0 && end >= 0 )
    {
       if ( wyslij.getCaretPosition( ) == start )
          {
           wyslij.setCaretPosition( end );
           wyslij.moveCaretPosition( start );
          }
       else
          {
           wyslij.select( start, end );
          }
    }

}

public void focusLost(FocusEvent arg0) {

        start = wyslij.getSelectionStart( );
        end = wyslij.getSelectionEnd( );

}

public void actionPerformed(ActionEvent e) {

    if(e.getActionCommand().equals("b")){

        int len = doc1.getLength();

        try {

            podziel = all.substring(stringStart,doc1.getLength());
            listaString.add(podziel);
            //listaStyl.add(in.toString());
            listaStyl.add(in);
            doc1.remove(0,doc1.getLength());
            j=0;
            for(int i=0;i<listaString.size();i++){
                System.out.println(" STYL "+i+" =");
            }

            for(int i=0 ; i<listaString.size();i++){
            doc2.insertString(doc2.getLength(), (String)listaString.get(i)+"\n" , (AttributeSet) listaStyl.get(i));

            }

        } catch (BadLocationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        /*System.out.println("LISTA = "+listaString.size());
        System.out.println("LISTA STYLI= "+listaStyl.size());
        for(int i=0;i<listaString.size();i++){
            System.out.println(listaStyl.get(i));
        }*/
        listaString.clear();
        stringStart = 0;

        listaStyl.clear();

    }

}

public void caretUpdate(CaretEvent e) {

    System.out.println(e.getDot());

    try {

        all = doc1.getText(0,doc1.getLength());

    if(j==0)
    {
        spr = set;
        listaStyl.add(in);
        System.out.println(listaStyl.get(0));

    }
    j++;
    if(e.getDot()>=2){

        if(spr == set){
        System.out.println("row");

    }else{

        System.out.println(" niew row");
        spr = set;
        podziel = all.substring(stringStart,doc1.getLength());
        stringStart = doc1.getLength();
        listaString.add(podziel);
        listaStyl.add(in);
        System.out.println(listaStyl.get(0));
        //listaStyl.getClass().

        }
    }
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

}