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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable TableModel getValueAt when using CachedRowSet 1

Status
Not open for further replies.

Custom24

Programmer
Nov 27, 2001
591
GB
Hi
I've written a TableModel to support my JTable. It takes a CachedRowSet in its constructor, since this is the data I am trying to show.

The CachedRowSet does not seem to have a method to move the cursor to an arbitrary row, even though all the data is in memory, which is peculiar. So, in the getValueAt method, I think I only have two approaches.

1. Move back to the first record, and count down to the requested row. This is what I currently am doing. I guess I could make it more efficient by checking if the row is the same as the last time the method was called, or if it is just the next one, in which case I don't have to move back to the start.

2. Hold the data in an array or vectors or whatever. This seems inefficient because the data is taking up memory twice.

Has anyone any better ideas? Here is my current code. You can see I have another issue in the code with throwing exceptions back to the caller, but I am not so worried about that right now.

Thanks

Code:
        public Object getValueAt(int row, int col) {
        //unfortunately, there does not seem to be a method in the CachedRowSet to do this, so it seems that the way is
        //to move back to the start of the set, and count down the rows.  That sucks - there must be a better way - we have all the 
        //data in memory, for Pete's sake!
        
        //again, similar to getColumnName - a flag
        Object o = "#Error in getValueAt for row " + row + " and col " + col;
        try{
        m_crs.first();
        for (int i = 1; i <= row ; i++) 
        {
          m_crs.next();
        }    
            o =  m_crs.getObject(col + 1);
        }
        
                    catch (Exception e)
            {
              m_client.ErrorOccured(e);
            }
            return o;
            }

Mark [openup]
 
without having used CachedRowSet, I found in the Javadocs:

absolute

boolean absolute(int row)
throws SQLException

Moves the cursor to the given row number in this ResultSet object.
...
in ResultSet, which is a Superinterface of CRS.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top