🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9521

Re: hide rows in JTable?

#9521
From: John P. McGrath
Date: Tue, 07 Mar 2000 00:00
83 lines
3869 bytes
On 05 Mar 2000 10:39:31 +0100, Tor Iver Wilhelmsen wrote:

> John P. McGrath <JPMcGrath@fast.net> writes:
> 
> > Those two are all you need to change if everything is guaranteed to be 
> > static.  But if the data can change, the job gets more complicated.  
> 
> No, because you change the _model's_ getRowCount() and getValueAt()
> methods, not the JTable's.

I don't see why that would indicate that you do not need to do further 
row number mapping in other methods.

For the sake of discussion, lets assume that the table model is storing 
the values in a List of Lists called "data".  We have to store all of the 
data, included those that are filtered, since we have to be able to turn 
the filter off.

And for simplicity, let's assume that the only filter we can apply is one 
that eliminates all of the odd row numbers.  The following code might be 
used to implement this:

    int getRowCount() {
        return ( filtering ? ((rowCount+1)/2) : rowCount );
    }

    int mapToUnfilteredRow( int fRow ) {
        return ( filtering ? (fRow * 2) : fRow );
    }

    Object getValueAt( int fRow, int col ) {
        int uRow = mapToUnfilteredRow( fRow );
        return data.get(uRow).get(col);
    }

Clearly, the setValueAt() method for a given row number should set the 
same value that is retrieved by getValueAt() with that row number.  So it 
would look something like:

    void setValueAt( Object newValue, int fRow, int col ) {
        int uRow = mapToUnfilteredRow( fRow );
        return data.get(uRow).set(col,newValue);
    }

> > But you also need to listen for changes in the underlying table model, 
> 
> No, because that's the one you're working in.

My original reply suggested two alternatives -- one was to build the 
filtering into an existing TableModel and the other was to build a 
separate filtering table model.  The filtering table model approach is 
obviously a more general approach, since it could be used to filter any 
table model.  The line you quoted clearly refers to the separate table 
model approach, since there would be no "underlying table model" in the 
other case.

If you were building a separate, filtering data model, you would replace 
the List get() and set() calls with calls to the setValueAt() and 
getValueAt() methods of the underlying table model.

But building the filtering into the table model does not obviate the need 
for mapping the row numbers.  If row 10 in the unfiltered data changed, 
and you fired an update for row 10, the JTable would be update filtered 
row number 10, rather than 5 as is should do.  So you would also need 
some methods that look like this:

    boolean isRowFiltered( int uRow ) {
        return ( filtering && ((uRow % 2) == 1) );
    }

    int mapToFilteredRow( int uRow ) {
        //  This assumes that isFiltered(uRow) is false.
        return ( filtering ? (uRow / 2) : uRow );
    }

Then, the TableModelListener notification code would have to determine if 
the row(s) in question are currently filtered and perform the appropriate 
mapping to filtered row numbers, using the above methods.

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.132e7732dbedd15b9897dc@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <38B969F1.FC38DF02@db.erau.edu> <MPG.13236cbbe6eb6d2a9897c7@forums.borland.com> <89qbui$r9s4@bornews.borland.com> <uk8jizsx7.fsf@sds.no> <MPG.132befc0c63e2db9897d9@forums.borland.com> <u66v1wyb0.fsf@sds.no>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,8e0f666ae34fba12
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: hide rows in JTable?
Date: 2000/03/07
Message-ID: <MPG.132e7732dbedd15b9897dc@forums.borland.com>
X-Deja-AN: 594559208
References: <38B969F1.FC38DF02@db.erau.edu> <MPG.13236cbbe6eb6d2a9897c7@forums.borland.com> <89qbui$r9s4@bornews.borland.com> <uk8jizsx7.fsf@sds.no> <MPG.132befc0c63e2db9897d9@forums.borland.com> <u66v1wyb0.fsf@sds.no>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing