serializing subclass of JScrollPane


2005-01-01 03:06:13 AM
jbuilder9
Hi there,
Please help me to properly set up serialization/deserialization of a
subclass of JScrollPane. I called the class Listbox. It extends JScrollPane
implements Serializable and contains a JList object. I have a start on this
but I'm not getting the results I need. Without implementing readObject(...)
and writeObject(...) methods, I keep losing the text inside the JList. It's
still there, just that it disappears everytime I do a save.
I tried implementing the readObject(...) and writeObject(...) methods, but
the text in the JList still keeps disappearing after every save to file and
the visual component of the JScrollPane goes missing when I open a saved
file.
Below is my class Listbox.
Please advise,
Alan
*************************************************
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.MouseEvent;
import java.io.*;
public class Listbox extends JScrollPane implements InputTool, Serializable
{
private JList list = null;
private String id = "";
private String[] values = null;
private static final String[] EMPTY_ARRAY = new String[0];
private boolean haveValues;
private int item = 0; //default
private InputType type = null;
private int componentIndex = -1;
public Listbox()
{
list = new JList();
super.getViewport().add(list);
list.setVisibleRowCount(4);
setPreferredSize(new Dimension(140,90));
setMaximumSize(new Dimension(140,90));
ItemListener listener = new ItemListener();
list.addMouseListener(listener);
setMultipleSelect(false);//default
}
public int getItemNumber(){return item;}
public void setItemNumber(int number)
{
item = number;
}
public InputType getInputType(){return type;}
public void setInputType(InputType type){this.type = type;}
public JList getList()
{
return list;
}
public String[] getOptions()
{
ListModel model = list.getModel();
if(model.getSize() == 0)return EMPTY_ARRAY;
String[] st = new String[model.getSize()];
for(int i = 0; i < model.getSize(); i++)
{
st[i] = (String)model.getElementAt(i);
}
return st;
}
public void setOptions(String[] options)
{
list.setListData(options);
}
public String[] getValues()
{
if(haveValues)
{
return values;
}
return EMPTY_ARRAY;
}
public void setValues(String[] values)
{
this.values = values;
haveValues = true;
}
public void setID(String ID)
{
this.id = ID;
}
public String getID()
{
return id;
}
public String getListSize()
{
return String.valueOf(list.getModel().getSize());
}
public boolean isMultipleSelect()
{
int value = list.getSelectionMode();
if(value == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
return true;
if(value == ListSelectionModel.SINGLE_INTERVAL_SELECTION)
return true;
return false;
}
public void setMultipleSelect(boolean value)
{
if(value)
{
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
else
{
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
public void setComponentIndex(int index)
{
componentIndex = index;
}
public int getComponentIndex()
{
return componentIndex;
}
private void readObject(ObjectInputStream in)throws IOException,
ClassNotFoundException
{
in.defaultReadObject();
id = in.readUTF();
values = (String[])in.readObject();
haveValues = in.readBoolean();
item = in.readInt();
type = (InputType)in.readObject();
componentIndex = in.readInt();
list.setModel((ListModel)in.readObject());
}
private void writeObject(ObjectOutputStream out)throws IOException
{
out.defaultWriteObject();
out.writeUTF(id);
out.writeObject(values);
out.writeBoolean(haveValues);
out.writeInt(item);
out.writeObject(type);
out.writeInt(componentIndex);
out.writeObject(list.getModel());
}
class ItemListener extends MouseInputAdapter implements Serializable
{
/*NOTE:
*Depending on the platform, either the mousePressed or mouseReleased
*event will be triggered. Testing has proven that in Windows the
*mouseReleased event gets triggered. Whereas on Linux, the
mousePressed
*event gets triggered.
*/
public void mousePressed(MouseEvent e)
{
JTestComposer.setEditor(Listbox.this);
checkForTriggerEvent(e);
}
public void mouseReleased(MouseEvent e)
{
checkForTriggerEvent(e);
}
private void checkForTriggerEvent(MouseEvent event)
{
if(event.isPopupTrigger())
{
JTestComposer.currentComponent = Listbox.this;
JTestComposer.popupMenu.show(event.getComponent(), event.getX(),
event.getY());
}
}
}
}