Handle Scrolling Events for JScrollBar in Java

Description

The following code shows how to handle Scrolling Events for JScrollBar.

Example


 /*from  ww  w  .j a v  a 2 s  .c om*/
import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JFrame;
import javax.swing.JScrollBar;


public class Main {
  public static void main(String args[]) {
    AdjustmentListener adjustmentListener = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
        System.out.println("Adjusted: " + adjustmentEvent.getValue());
      }
    };
    JScrollBar oneJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
    oneJScrollBar.addAdjustmentListener(adjustmentListener);

    JFrame frame = new JFrame("ScrollBars R Us");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(oneJScrollBar, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Handle Scrolling Events for JScrollBar in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer