I was recently at a customer site and had a client come to me with a Swing problem that at first seemed simpler than it was.  

They wanted a way to detect when the scrollbar on a particular JTable (e.g. the scrollbar on the JScrollPane wrapping it) reached the bottom.  I thought -- this shouldn't be hard -- just catch the scroll event and then interrogate the table to see if you are at the end.

Within a few minutes I had whipped up a demo using JLists that showed how it would work.  I showed it to one of my customers who then pointed out that the problem was that they were using TABLES not LISTS.  Undaunted, I looked at the JTable class and found some startling inconsistencies.

I knew that JTable and JList didn't share a common superclass lower than JComponent, but what I wasn't prepared for is how different the two protocols are.  Naively, I was expecting something like the way that VisualWorks Smalltalk worked -- where a list and a table were almost the same thing in many ways.

In particular, my problem was this -- JList implements two methods that made my task trivial for lists:

getLastVisibleIndex() -- returns the index of the last visible element

locationToIndex(Point) -- converts a point to an index

Strangely enough, JTable has ''nothing'' like these.  What I was able to do, however, was to steal the code from these two methods and directly apply it to my own class to write a method that took the visible rectangle and applied the same math the two above methods use to calculate the last visible row index.

It worked, but I still don't like it.  So my question is, am I wacky to expect these two classes to work in the same way?  Shouldn't there be some common interface that they both implement?  Or am I just too poisoned by my long exposure to a self-consistent, reasonable UI environment like VisualWorks :)

KyleBrown

----
CategoryJava