Board index » jbuilder » lost text

lost text


2004-12-30 03:57:58 AM
jbuilder3
Hi there,
In my project I save objects to file. One such object I call Listbox. It
extends JScrollPane and its viewport contains a JList.
It looks like so:
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;
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
}
...
}
In my SaveFile() method using SwingUtilities.invokeLater() method (see
below), I can save all the objects to file alright, but the text in my
Listbox keeps disappearing. I have tried calling repaint(), revalidate() on
both Listbox and the JList it contains, but neither works. The text is
there, it just isn't showing after a save. What am I doing wrong?
Alan
*********************************************
public void SaveFile()
{
//if FileOpen is blank then we know that the DocumentPanel
//hasn't been saved as a particular file yet. So, we move along
//to SaveFileAs() method in this case.
if(FileOpen.equals(""))
{
SaveFileAs();
return;
}
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
FileOutputStream fStream = null;
ObjectOutputStream stream = null;
try
{
File file = new File(FileOpen);
if(file.exists())
{
fStream = new FileOutputStream(file);
stream = new ObjectOutputStream(fStream);
stream.writeObject(docPanel);
}
}
catch(Exception ex)
{
ex.printStackTrace();
JOptionPane.showMessageDialog(JTestComposer.this, "Save
Error: " + ex.toString(),
"Warning", JOptionPane.WARNING_MESSAGE);
}
finally
{
try
{
if(stream != null)
stream.close();
if(fStream != null)
fStream.close();
InputType type = null;
for(int i = 1; i <= docPanel.getTable().getRowCount();
i++)
{
type =
docPanel.getTable().getTableRow(i).getTargetPanel().getInputType();
if(type == InputType.LISTBOX)
{
Listbox list =
(Listbox)docPanel.getTable().getTableRow(i).getTargetPanel().getComponent(0)
;
list.revalidate(); //<< this is where I'm trying to
get the text to reappear
}
}
}
catch(IOException ioe){}
}
}
});
}
 
 

Re:lost text

Looks to me like what your doing is serializing/de-serializing the Visual
Component and not the ListModel. Whats happening is that when you
de-serialize, the data is loaded into the View but then its overwritten by
the Model, which has not been loaded. This is why I always ser./de-ser. the
Model and it will update the View. Why do you need to serialize the View
and not the Model?
If you need to serialize a container full of visual components, then you
need to split it up into serializing the models of the components, that have
one, and then serialize the attributes of the panel itself. Make sense?
And do it in the right order, serialize vc models, then serialize panel
attributes, and then reverse the order for de-ser so the models get loaded
last and they update the views.
GM
 

Re:lost text

"GUI-Majician" < XXXX@XXXXX.COM >wrote in message
Quote
Looks to me like what your doing is serializing/de-serializing the Visual
Component and not the ListModel. Whats happening is that when you
de-serialize, the data is loaded into the View but then its overwritten by
the Model, which has not been loaded. This is why I always ser./de-ser.
the
Model and it will update the View. Why do you need to serialize the View
and not the Model?
I've never had an occasion when I ever had to serialize anything for
purposes of saving to file. This is my first time trying this. I didn't know
that, when serializing/deserializing an object, that had a model, that you
concentrated on just the model of that object.
Quote

If you need to serialize a container full of visual components, then you
need to split it up into serializing the models of the components, that
have
one, and then serialize the attributes of the panel itself. Make sense?
If this is the way it is done, then I can only accept your analysis.
Quote

And do it in the right order, serialize vc models, then serialize panel
attributes, and then reverse the order for de-ser so the models get loaded
last and they update the views.
Given a container that contains other objects, such as my Listbox object,
could you show me a simple demo program that demonstrates the
serializing/deserializing of the objects model? I need a sample to go by to
see how it is done. I've provided my Listbox class below for your
convenience.
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;
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);
}
}
class ItemListener extends MouseInputAdapter implements Serializable
{
public void mousePressed(MouseEvent e)
{
JTestComposer.setEditor(Listbox.this);
}
}
}
 

{smallsort}