Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

Manipulating the size of the track and knob in a scrollbar of a JTable

Thread view: 
Dave - 15 Nov 2003 19:28 GMT
Hello there,

I have a JTable added to a JScrollPane. The JTable shows the contents of a
databasetable. I only want to load the datarows from the database when they
are needed (meaning: when they are in the visible area of the JTable),
because the JTable could show only 10 rows, while the databasetable could
contain thousands of rows. I have accomplished this by overriding the
getScrollableBlockIncrement and getScrollableUnitIncrement methods of
JTable, from where data is loaded from the database. The size of JTable's
datamodel is therefore always the size of the rows visible. I have set the
vertical scrollbar to be always visible. Furthermore, the datamodel holds a
datastructure with the primary key values for all the rows in the
databasetable; this allows me to fetch the complete datarow when I need it.

Now I have the following problem: the size of the knob on the vertical
scrollbar in relation to the size of the track is based on the number of
rows visible and the number of rows in the datamodel of JTable (at least,
that's my conclusion), and this is obvious not what I want. I want the size
of the knob and the size of the track to be related to the rowcount of the
databasetable and not to the rowcount of the datamodel.

I hope there is someone out there who can give me some pointers in how to
accomplish this? Also suggestions that take a completely different approach,
but prove to be working, are much appreciated.

Thanks in advance.

Dave

---------------------------------------------------------------------
Computers, you can't live with them and you can't live without them.
---------------------------------------------------------------------
Dave - 23 Nov 2003 21:20 GMT
> Hello there,
>
[quoted text clipped - 24 lines]
>
> Dave

I have found the solution myself and will post it here for those people, now
or in the future, who run into the same problems as I did.

I first tried the following scenario:
Intervene with the drawing of the scrollbar by feeding the scrollbar's
underlying BoundedRangeModel with numbers that reflect the rowcount in the
database and not of the table, because the table only holds the rows that
are visible in the viewport. However, I had troubles, even after
investigating the j2sdk sourcecode, to understand the communication between
the scrollbar and the JTable.

 So I took another approach. This one turned out to work for me. I had to
do the following things:
 (1) Create an attribute in your TableModel that records the first row in
the viewport (that is, the first visible row) and one that records the
maximum number of rows that are visible in the viewport. Let the TableModel
manage those attributes.
 (2) Let the TableModel return the number of rows in the databasetable and
not the number of rows in the JTable when getRowCount() is called on
TableModel.
 (3) Let the data in the TableModel be the data that is visible in the
viewport of the JTable and nothing more.
 (4) Register the TableModel as a listener to the BoundedRangeModel of the
vertical JScrollBar. Scrolling the scrollbar, either by sliding the thumb,
clicking the track or clicking the decrease- or increasebutton, triggers
change-events that will be send to the TableModel after registering.
 (5) Handle change-events from the BoundedRangeModel in the
stateChanged-method of the TableModel. From the change-events, the source
can be queried, which will return the BoundedRangeModel. From the
BoundedRangeModel, the value of the model can be retrieved and it represents
the topY-position of the view in the viewport. (int topY =
e.getSource().getValue(); where e is the change-event). By translating the
topY to a value that represents the new first displayed row, you can
retrieve new data from your databasetable when necessary.
 (6) Translate the rowindex-arguments in getValueAt and setValueAt of the
TableModel to an index in the data of the JTable.
I hope this info is understandable and usefull for those who want to
minimize the memory footprint of a JTable that displays data from a
databasetable, without sacrificing userfriendlyness by obscure behaviour of
the vertical scrollbar. Please take into account that my solution presumes
equal rowheight for each row in the JTable. This is not necessary, but sure
makes life easier.

Regards,

Dave


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.