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);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    bar.addAdjustmentListener(this);
    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.add(bar, "South");
    setContentPane(pane);
  }

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

  public void adjustmentValueChanged(AdjustmentEvent evt) {
    Object source = evt.getSource();
    int newValue = bar.getValue();
    System.out.println(newValue);
    repaint();
  }
}

   
  








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