devxlogo

Scrollbar Value Notification

Scrollbar Value Notification

Question:
Is there a way that I can dynamically determine the value of aScrollbar when I am scrolling? The adjustmentValueChanged method gets called only when you stop scrolling. But I want to catchthe value of the scrollbar when I press on the scrollbar and Iam dragging it. Is there a way to do this?

Answer:
As most Java programmers know by now, you pay a price for thecrossplatform portability of Java. Your code may compile onceand run everywhere, but it may not behave the same way on everyplatform. The Scrollbar class is an AWT component in which the ultimaterepresentation and behavior is determined by a native peer.

InWindows 95, you may find that the behavior of a Scrollbar is tonotify its listeners of a change in value only after it has finished moving, rather than smoothly while it’s moving. In aUnix/Motif environment, I find that each time the Scrollbar movesa unit increment, the listener is notified. Rather than rely onnative peers, it is better to use the Java Foundation Classes,specifically the Swing package, to guarantee consistent behavioracross platforms. The following example program allows youto test the behavior of java.awt.Scrollbar versus that ofjavax.swing.JScrollBar. A listener is registered with a scrollbarof each type, and prints out the scrollbar values each time it isnotified of a change via the AdjustmentListener interface.

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ScrollTest {  public static class ScrollTestComponent extends JPanel    implements AdjustmentListener  {    JTextArea _scrollText;    Scrollbar _scrollBar;    JScrollBar _jscrollBar;    public ScrollTestComponent() {      _scrollBar  = new Scrollbar();      _jscrollBar = new JScrollBar();      _scrollBar.addAdjustmentListener(this);      _jscrollBar.addAdjustmentListener(this);      _scrollText = new JTextArea(40, 40);      _scrollText.setEditable(false);      setLayout(new BorderLayout());      add(_scrollBar, BorderLayout.WEST);      add(_scrollText, BorderLayout.CENTER);      add(_jscrollBar, BorderLayout.EAST);      printScrollValues();    }    public void printScrollValues() {      _scrollText.setText("    ScrollBar: ");      _scrollText.append(Integer.toString(_scrollBar.getValue()));      _scrollText.append("
    JScrollBar: ");      _scrollText.append(Integer.toString(_jscrollBar.getValue()));    }    public void adjustmentValueChanged(AdjustmentEvent e) {      printScrollValues();    }  }  public static void main(String[] args) {    JFrame frame = new JFrame("Scroll Test");    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }    };    frame.addWindowListener(exitListener);    frame.getContentPane().add(new ScrollTestComponent(),                               BorderLayout.CENTER);    frame.pack();    frame.setVisible(true);  }}
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist