Author: Unicman
Author's WebSite: http://members.tripod.com/unicman
Use of the component
ScrollBarSyncronizer is very useful for syncronizing two scroll bars of any scrollable area.
Design of VariableString
ScrollBarSyncronizer class is a change adapter actually. Constructor of it takes scrollbar instance as parameter.
This syncronizer if attached to other scrollbar, syncronizes the other scrollbar (the one provided in constructor) according to the position of this scrollbar.
Code below shows how to use ScrollBarSyncronizer:
/**
* @(#) TestScrollBarSync.java 1.0 99/08/29
*
* This code is designed for JDK1.2
* Use tab spacing 4. Follow JavaDoc convention while coding.
* Mail any suggestions or bugs to unicman@iname.com
*/
import um.util.ScrollBarSynchronizer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Simple panel to test scroll bar syncronizer.
*
* @author UnicMan
* @version 1.0 08/29/99
*/
public class TestScrollBarSync
extends JPanel
implements WindowListener
{
public String[] aszItems1 =
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8",
"Item 9",
"Item 10",
"Item 11",
"Item 12",
"Item 13",
"Item 14",
"Item 15",
"Item 16",
"Item 17",
"Item 18",
"Item 19",
"Item 20",
"Item 21",
"Item 22",
"Item 23",
"Item 24",
"Item 25",
"Item 26",
"Item 27",
// "Item 28", // If these two lines are uncommented,
// "Item 29", // size of both lists will be same. So
// // even if the list is scrolled, rows of
// // both lists will be in front of each other.
};
public String[] aszItems2 =
{
"Element 1",
"Element 2",
"Element 3",
"Element 4",
"Element 5",
"Element 6",
"Element 7",
"Element 8",
"Element 9",
"Element 10",
"Element 11",
"Element 12",
"Element 13",
"Element 14",
"Element 15",
"Element 16",
"Element 17",
"Element 18",
"Element 19",
"Element 20",
"Element 21",
"Element 22",
"Element 23",
"Element 24",
"Element 25",
"Element 26",
"Element 27",
"Element 28",
"Element 29",
};
public TestScrollBarSync()
{
super( new BorderLayout() );
JList jl1 = new JList( aszItems1 );
JList jl2 = new JList( aszItems2 );
JScrollPane sc1 = new JScrollPane( jl1 );
JScrollPane sc2 = new JScrollPane( jl2 );
sc1.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
////
// Code for attaching syncronizer...
JScrollBar sb1 = sc1.getVerticalScrollBar();
JScrollBar sb2 = sc2.getVerticalScrollBar();
sb2.getModel().addChangeListener( new ScrollBarSynchronizer( sb1 ) );
//
////
JSplitPane sp = new JSplitPane();
sp.setLeftComponent( sc1 );
sp.setRightComponent( sc2 );
add( sp, BorderLayout.CENTER );
}
public static void main( String arg[] )
{
JFrame jf = new JFrame();
TestScrollBarSync ss = new TestScrollBarSync();
jf.setContentPane( ss );
jf.setSize( 300, 200 );
jf.setLocation( 50, 50 );
jf.addWindowListener( (WindowListener)ss );
jf.show();
}
public void windowActivated( WindowEvent weEvent ) {}
public void windowDeactivated( WindowEvent weEvent ){}
public void windowIconified( WindowEvent weEvent ) {}
public void windowDeiconified( WindowEvent weEvent ){}
public void windowOpened( WindowEvent weEvent ) {}
public void windowClosed( WindowEvent weEvent ) {}
public void windowClosing( WindowEvent weEvent )
{
System.exit(0);
}
}
Code for ScrollBarSyncronizer is very small...
/**
* @(#) ScrollBarSynchronizer.java 1.0 99/08/28
*
* This code is designed for JDK1.2
* Use tab spacing 4. Follow JavaDoc convention while coding.
* Mail any suggestions or bugs to unicman@iname.com
*/
package um.util;
//import com.sun.java.swing.JScrollBar; //**JDK115**//
//import com.sun.java.swing.BoundedRangeModel; //**JDK115**//
//import com.sun.java.swing.event.ChangeListener; //**JDK115**//
//import com.sun.java.swing.event.ChangeEvent; //**JDK115**//
import javax.swing.JScrollBar; //**JDK12**//
import javax.swing.BoundedRangeModel; //**JDK12**//
import javax.swing.event.ChangeListener; //**JDK12**//
import javax.swing.event.ChangeEvent; //**JDK12**//
/**
* This class is used for synchronizing two JScrollBars.
* <p>
* If (maximum - minimum) of the scroll bars don't match, the other scrollbar
* will be adjusted proportionally.
*
* @Author UnicMan
* @version 1.0 99/08/28
*/
public class ScrollBarSynchronizer
implements ChangeListener
{
private JScrollBar msbScrollBar;
/**
* Constructor.
*
* @param destScroll ScrollBar to synchronize
*/
public ScrollBarSynchronizer( JScrollBar destScroll )
{
msbScrollBar = destScroll;
}
public void stateChanged( ChangeEvent e )
{
BoundedRangeModel sourceScroll = (BoundedRangeModel)e.getSource();
int iSMin = sourceScroll.getMinimum();
int iSMax = sourceScroll.getMaximum();
int iSDiff = iSMax - iSMin;
int iSVal = sourceScroll.getValue();
int iDMin = msbScrollBar.getMinimum();
int iDMax = msbScrollBar.getMaximum();
int iDDiff = iDMax - iDMin;
int iDVal;
if( iSDiff == iDDiff )
iDVal = iSVal;
else
iDVal = (iDDiff * iSVal) / iSDiff;
msbScrollBar.setValue( iDMin + iDVal );
}
}
Download demo classes
Download source file of ScrollBarSyncronizer
Posted On: 29-Aug-1999