|
|
| |
"Scrolling on Demand"
Vol. 9, Issue 7, p. 40
Listing 1: installUI() from ScrollableBarUI.java
public void installUI(JComponent c) {
sb = (ScrollableBar)c;
inc = sb.getIncrement();
boolean small = sb.isSmallArrows();
// Create the Buttons
int sbSize =
((Integer)(UIManager.get( "ScrollBar.width" ))).intValue();
scrollB = createButton(sb.isHorizontal()?WEST:NORTH,
sbSize, small);
scrollB.setVisible(false);
scrollB.addMouseListener(this);
scrollF = createButton(sb.isHorizontal()?EAST:SOUTH,
sbSize, small);
scrollF.setVisible(false);
scrollF.addMouseListener(this);
int axis = sb.isHorizontal()?BoxLayout.X_AXIS:BoxLayout.Y_AXIS;
sb.setLayout(new BoxLayout(sb, axis));
scroll = new JViewport() {
... see source code ...
};
Component box = sb.getComponent();
scroll.setView(box);
sb.add(scrollB);
sb.add(scroll);
sb.add(scrollF);
// Install the change listeners
scroll.addChangeListener(this);
sb.addPropertyChangeListener(this);
}
Listing 2: mousePressed() from ScrollableBarUI.java
public void mousePressed(MouseEvent e) {
pressed = true;
final Object o = e.getSource();
Thread scroller = new Thread(new Runnable() {
public void run() {
int accl = 500;
while (pressed) {
Point p = scroll.getViewPosition();
... Compute new view position ...
scroll.setViewPosition(p);
try {
Thread.sleep(accl);
if (accl <= 10) accl = 10;
else accl /= 2;
} catch (InterruptedException ie) {}
}
}
});
scroller.start();
}
|
|
All Rights Reserved
Copyright © 2004 SYS-CON Media, Inc.
E-mail: info@sys-con.com
Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.
|