advice on JTables and models
First I'll explain the situation and how I'm currently doing things. I would like someone's feedback on whether they think this is a good method or not. If not, how should I do it.
I have a vector that contains all my objects, simply a library of data. I don't need to display all the data. Certain criteria can limit the results shown in the table. Users can either search through the library, or use a playlist. A playlist contains a vector which holds integers, indices to main data vector. My table model requires 3 parameters, a data vector, vector of columns, and a view vector containing integers. I use this view vector to point to the indices of the main data vector. So my table model looks something like this:
public int getRowCount()
public Object getValueAt(int row, int column)
//translate index from view Vector into relations of the
//Vector holding the main data
row = ((Integer)view.get(row)).intValue();
//retrieve data as usual
My main class would then change the view vector using whatever filters I have implemented. I search through the main data Vector and if an item meets my criteria, I add its index into the view Vector. When changes are made to that, I fire a data change event on the table model.
Sorting is done on the view vector so that the main data vector remains in the same order. When data is added to the main vector, it is simply appended at the end.
The only problem I can see is keeping things synchronized. The playlists are built by selecting items from the main data and saving the indices that point to them. If an item should be removed from the main data vector, the entire playlist could result in incorrect data. It wouldn't be hard to implement a method that updated all the playlists for when an item is removed from the main data vector, but I thought someone might have a better way. I appreciate anyone who reads this whole post and can offer a bit of insight.
one way you could do it is create a new object containing both vectors.
Then you could just make calls to that object, keeping a "current row"index as well.
That way, each request you make, requests the "current row".
The current row is relational to your ordered vector (the one with the indices).
When you want to remove a row, you can just remove the current row, thus shifting the current row index down.
Another solution could be to implement a "VectorListener". I.e. making a
java.util.Vector extension (BroadCastVector ?) that has an iinner ArrayList/Vector of
listeners. You would then define an interface, VectorListener that specifies the
public void vectorChanged(int changeType, Object value).
And the BroadCastVector would have two methods:
public void addVectorListener(VectorListener vl)
public void removeVectorListener(VectorListener vl)
Then you could wrap the updatmethods for java.uitil.Vector in your own
implementations and for each invokation of these, scan the list of listeners and
broadcast what has happened to the vector.
In short: your table model implements VectorListener.
I've used this approach many times, it's clean and easy to maintain.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL