Swing JScrollBar Class



Swing JScrollBar Class :

package com.c4learn.swing;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
public class JScrollDemo {
  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;
  public JScrollDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(300, 300);
    mainFrame.setLayout(new GridLayout(3, 1));
    headLabel = new JLabel("Scroll Demo", JLabel.CENTER);
    msgLabel = new JLabel("No Action",JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());
    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }
  public static void main(String[] args) {
    JScrollDemo swingControlDemo = new JScrollDemo();
    swingControlDemo.showScrollDemo();
  }
  private void showScrollDemo() {
    final JScrollBar hScroll = new JScrollBar(0);
    final JScrollBar vScroll = new JScrollBar();
    vScroll.setOrientation(JScrollBar.VERTICAL);
    hScroll.setMaximum(100);
    hScroll.setMinimum(1);
    vScroll.setMaximum(100);
    vScroll.setMinimum(1);
    hScroll.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        int hValue = hScroll.getValue();
        int vValue = vScroll.getValue();
        msgLabel.setText("Horizontal : " + hValue + 
            " & Vertical : "  + vValue);
      }
    });
    vScroll.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        int hValue = hScroll.getValue();
        int vValue = vScroll.getValue();
        msgLabel.setText("Horozontal : " + hValue + " " +
            " & Vertical : "  + vValue);
      }
    });
    mainPanel.add(hScroll);
    mainPanel.add(vScroll);
    mainFrame.setVisible(true);
  }
}

Output :

scroll_swing