Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Data Models - JList 2

Status
Not open for further replies.

hganywhere

Programmer
Jan 10, 2005
6
DK
I would like to use a JList to display employee information and allow add/delete operations on this JList.

I am having a problem when adding/removing a record from the JList, the model is being updated but the JList goes blank.

I do not use Arrays, Vectors etc to maintain my model. My model is an ArrayList of EmployeeVO's.

An EmployeeVO class will have fields such as :

EmpId
EmpFirstName
EmpLastName

etc.

The EmployeeVO also has a toString() method that allows for displaying of the EmployeeVO inside the JList.

The Model is defined as :

//
// Our Employee Model
//
class EmployeeListModel2 extends AbstractListModel
{
private EmployeeListCollection collection=null;
EmployeeListModel2(EmployeeListCollection c)
{
this.collection=c;
}

public int getSize()
{
return collection.getEmployeeCount();
}

public Object getElementAt(int index)
{
return collection.getDisplayItem(index);
}

public void addElement(Object o)
{
int index=collection.getEmployeeCount();
collection.addElement(o);
fireIntervalAdded(this,index,index);
}

public ArrayList getElements()
{
return collection.getEmployeeList();
}

public void addListDataListener(ListDataListener l)
{

}

public void removeListDataListener(ListDataListener l)
{
}
}


The EmployeeListCollection class is defined as

//
// Data Structure to hold employee list
//
class EmployeeListCollection
{
private ArrayList employeeList=null;
EmployeeListCollection(ArrayList l)
{
this.employeeList=l;
}

public void addElement(Object o)
{
System.out.println("EmployeeListCollection.addElement - "+o);
employeeList.add(o);
}
public int getEmployeeCount()
{
return employeeList.size();
}
public ArrayList getEmployeeList()
{
return employeeList;
}

public Object getDisplayItem(int index)
{
return (EmployeeVO)employeeList.get(index);
}
}


The code to add an item is defined as :

public void __add(EmployeeVO vo)
{
int size=model.getSize();
model.addElement(vo);
}

Does anyone have any ideas ?

P.s, I do not get a problem if I use DefaultListModel or use a model that is based on an Array or Vector.

TIA

 
You should also provide a call to the superclass in your overriden listener methods in your model, thus :-
Code:
public void addListDataListener(ListDataListener l){
   super.addListDataListener(l);
}

public void removeListDataListener(ListDataListener l){
   super.removeListDataListener(l);
}

..or better still, take these two methods out completely unless you intend to extend their implementation; the abstract superclass will do their job for you.

Incidentally, since you still have to add elements to your model class via a non-type safe addElement(Object o) method your not getting anything by subclassing the AbstractListModel. You may as well use the DefaultListModel.

Tim
 
Tim, thanks alot your right I should have called the super methods, not quite sure how I managed to forget the implementation.

Re:
__
Incidentally, since you still have to add elements to your model class via a non-type safe addElement(Object o) method your not getting anything by subclassing the AbstractListModel. You may as well use the DefaultListModel.
__

Unless my understanding of DefaultListModel is incorrect, I cannot use this default implementation class since its persistence mechanism is a Vector and I do not wish to copy all my data from one structure into this data type.

My understanding of JList and its associated model interfaces/classes (ListModel, AbstractListModel, DefaultListModel) is based on :

Use


ListModel - If you want to have a Custom Data Model but do not need changes to the persistent store. (Display Only).

DefaultListModel - If you want to have add/remove/modify operations on a Vector persistent store.

AbstractListModel - If you want to provide your own persistent data store (Custom Data Store) and allow the persistent datastore to be modified. Shouldn't need to have expensive operations of copying from one persistence mechanism into a Vector (which is the requirement for DefaultListModel).

Thus, I have an ArrayList of EmployeeVO objects. An easy approach was to cycle through this arraylist and extract the EmployeeVO object and add it to the DefaultListModel's vector but this is an expensive overhead if there are 1000's of rows and also makes the idea of Custom Model's redundant; The View shouldn't dictate the Model, as long as I provide a mechanism to extract data from the model everything should be okay.

Thanks again for noticing my mistake.
 
I stand corrected [bigsmile]. Yes, you are getting better efficiency with your subclass of AbstractListModel. Thanks for correcting me.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top