DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004

    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()
        return view.size();
    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.

  2. #2
    Join Date
    Oct 2004
    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.

  3. #3
    Join Date
    Nov 2004
    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.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.