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);  }}
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap