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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

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

Sitemap