🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #10262

JTextPane remove icon problem

#10262
From: Laurent Kubanski
Date: Thu, 21 Oct 1999 00:00
158 lines
5511 bytes
Hi,

I need to serialize a JTextPane in order to put him in a object database;
however I know that is it not possible to serialize a Icon object. It is the
raison I remove icon before serialize (see the 'RUN KO' action):
            try
            {
                myTextPane.getStyledDocument().insertString(19, "\n", new
SimpleAttributeSet());
--->            removeIcon();  //REMOVE ICON BEFORE SERIALIZATION
--->            serialization(myTextPane.getStyledDocument());  //SERIALIZE
JTextPane WITHOUT ICONS
                myTextPane.repaint();
            }
            catch (Exeption exc)

First question: After this remove operation, my JTextPane don't contain Icon
object. Do you agree with that??
If yes, why I receive the 'MyIcon is not serializable as a value in an
AttributeSet' exeption???
If no, what is your explanation???

Second question: Do you know a workaround to serialize a icon object or/and
a JTextPane containing a icon??

Thanks you helping us,

QUARTZ System.
QSSA@Quartz-System.fr

SOURCE CODE :

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

public class MyFrame extends JFrame implements ActionListener
{
    public JTextPane myTextPane;

    public MyFrame()
    {
        super("MyFrame");
        getContentPane().setLayout(new BorderLayout());

        JButton myButton1 = new JButton("GOOD REMOVE ICON WITHOUT NEW
LINE");
        myButton1.addActionListener(this);
        myButton1.setActionCommand("RUN OK");
        getContentPane().add(BorderLayout.NORTH, myButton1);

        JButton myButton2 = new JButton("NO GOOD REMOVE ICON WITH NEW
LINE");
        myButton2.addActionListener(this);
        myButton2.setActionCommand("RUN KO");
        getContentPane().add(BorderLayout.SOUTH, myButton2);

        myTextPane = new JTextPane();
        myTextPane.setText("This is my textpane with a icon : ");
        myTextPane.select(myTextPane.getText().length(),
myTextPane.getText().length());
        myTextPane.insertIcon(new MyIcon());
        getContentPane().add(BorderLayout.CENTER, new
JScrollPane(myTextPane));

        setBounds(20, 20, 400, 200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("RUN OK"))


            removeIcon(); //REMOVE ICON BEFORE SERIALIZATION
            serialization(myTextPane.getStyledDocument()); //SERIALIZE
JTextPane WITHOUT ICONS
            myTextPane.repaint();
        }
        else if (e.getActionCommand().equals("RUN KO"))
        {
            try
            {
                myTextPane.getStyledDocument().insertString(19, "\n", new
SimpleAttributeSet()); ; //ADD NEW LINE
                removeIcon(); //REMOVE ICON BEFORE SERIALIZATION
                serialization(myTextPane.getStyledDocument()); //SERIALIZE
JTextPane WITHOUT ICONS
                myTextPane.repaint();
            }
            catch (Exception exc)
            {
                System.out.println("Exception : " + exc.getMessage());
            }
        }
    }

    private void removeIcon()
    {
        for (int i=0; i<myTextPane.getStyledDocument().getLength(); i++)
        {
            Element element =
myTextPane.getStyledDocument().getCharacterElement(i);
            AttributeSet attributeSet = element.getAttributes();
            Icon icon = StyleConstants.getIcon(attributeSet);
            if (icon != null)
            {
                try
                {
                    System.out.println("removeIcon : " + i + " = " + icon);
                    myTextPane.getStyledDocument().remove(i, 1);
                    myTextPane.getStyledDocument().insertString(i, " ", new
SimpleAttributeSet());
                    icon = null;
                }
                catch (Exception exc)
                {
                    System.out.println("Exception : " + exc.getMessage());
                }
            }
        }
    }

    private void serialization(Object object)
    {
        try
        {
            ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new
ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
        }
        catch (IOException exc) {System.out.println("IOException : " +
exc.getMessage());}
    }

    public static void main(String arg[])


        new MyFrame();
    }
}

class MyIcon implements Icon
{
    public MyIcon() {}
    public void paintIcon(Component c, Graphics g, int x, int y)


        g.setColor(Color.red);
        g.drawString("MyIcon", x, y+20);
    }
    public int getIconWidth() {return 100;}
    public int getIconHeight() {return 25;}
}
Technical Details
Message-ID: <7umee2$3si$1@minus.oleane.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,3ae162d68a5b7ec4,start
X-Google-Attributes: gid111d45,public
From: "Laurent Kubanski" <lkubanski@quartz-system.fr>
Subject: JTextPane remove icon problem
Date: 1999/10/21
Message-ID: <7umee2$3si$1@minus.oleane.net>
X-Deja-AN: 538738032
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
X-Complaints-To: abuse@oleane.net
X-Trace: minus.oleane.net 940489986 3986 62.160.166.95 (21 Oct 1999 07:13:06 GMT)
Organization: Guest of OLEANE
NNTP-Posting-Date: 21 Oct 1999 07:13:06 GMT
Newsgroups: borland.public.jbuilder.java.swing