HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"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: [email protected]

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.