 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave Guest
|
Posted: Sat Nov 15, 2003 7:28 pm Post subject: Manipulating the size of the track and knob in a scrollbar o |
|
|
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.
---------------------------------------------------------------------
|
|
| Back to top |
|
 |
Dave Guest
|
Posted: Sun Nov 23, 2003 9:20 pm Post subject: Re: Manipulating the size of the track and knob in a scrollb |
|
|
"Dave" <Davel*removethistosendemail*@superstar.demon.nl> wrote
| Quote: | 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
|
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|