Use Adjustment Events in Swing : ScrollBar « Swing JFC « Java

Use Adjustment Events in Swing


import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

public class Main extends JFrame implements AdjustmentListener {
  JScrollBar bar = new JScrollBar(SwingConstants.HORIZONTAL, 50, 10, 0, 100);

  public Main() {
    setSize(350, 100);
    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.add(bar, "South");

  public static void main(String[] arguments) {
    JFrame frame = new Main();

  public void adjustmentValueChanged(AdjustmentEvent evt) {
    Object source = evt.getSource();
    int newValue = bar.getValue();


Related examples in the same category

1.Accessible Scroll Demo Accessible Scroll Demo
2.A quick demonstration of JScrollBar both vertical and horizontalA quick demonstration of JScrollBar both vertical and horizontal
3.JScrollPane: Button Corner SampleJScrollPane: Button Corner Sample
4.JScrollPane CornerJScrollPane Corner
5.Expandable SplitPaneExpandable SplitPane
6.ScrollBar PiecesScrollBar Pieces
7.Listening for Scrollbar Value Changes in a JScrollPane Container
8.Get the default scrollbar policy
9.Make the scrollbars always appear
10.Make the scrollbars never appear
11.JScrollBar and Adjustment event
12.JScrollPane to hold scrollable component
13.Always display scrollbar
14.How to use scrollbar and react to its actionHow to use scrollbar and react to its action