3

I have a requirement of having N number of tables arranged in a grid pane side by side provided height of the scrollpanes of all the jtables remains same. Now i want to synchronize scroll of all the jtables means if i scroll the first jtable then rest of the jtables should scroll automatically by the same amount.

Anyone has any idea about this ?

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Sanket Pipariya
  • 79
  • 1
  • 12

2 Answers2

10

Try sharing the model of the scrollbar. Something like:

JScrollPane scrollPane1 = new JScrollPane(...);
JScrollPane scrollPane2 = new JScrollPane(...);
BoundedRangeModel model scrollPane1.getVerticalScrollBar().getModel();
scrollPane2.getVerticalScrollBar().setModel( model );
camickr
  • 308,339
  • 18
  • 152
  • 272
  • Thanks @camickr .. Code is very useful – Sanket Pipariya Aug 22 '13 at 20:09
  • camickr - how to unshare model of all n tables after share the model. Actually i have 1 synchronize checkbox in my screen. on click of checkbox i have to synchronize n number of table and and on uncheck of checkbox i have to unshare model of all the n tables. thats my question. do you have any idea about this ? – Sanket Pipariya Aug 25 '13 at 16:47
  • Create and add an new BoundedRangeModel to the scrollbar. – camickr Aug 25 '13 at 17:34
  • camickr - BoundedRangeModel is an interface. How can i create the object of BoundedRangeModel and add it into scrollbar ? – Sanket Pipariya Aug 25 '13 at 17:58
  • Read the API and find a class that implements the interface. All models have a default implementation. – camickr Aug 25 '13 at 18:03
  • I found the solution by adding new scrollbars: scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL)); – Sanket Pipariya Aug 25 '13 at 19:11
0

Get the scrollbars of your scrollpanes (getHorizontalScrollBar() or getVerticalScrollBar()). Then catch events from these. When an event occurs, modify the viewports (getViewport()) associated to the scrollpanes with setViewPosition() (from getViewPosition()).

Jean-Baptiste Yunès
  • 30,872
  • 2
  • 40
  • 66