SWING - JScrollBar Class

The class JScrollBar is an implementation of scrollbar.
Following is the declaration for javax.swing.JScrollBar class −
public class JScrollBar
   extends JComponent
      implements Adjustable, Accessible

Following are the fields for javax.swing.ImageIcon class −
protected int blockIncrement
protected int blockIncrement
protected BoundedRangeModel model − The model that represents the scrollbar's minimum, maximum, extent (aka "visibleAmount") and current value.
protected BoundedRangeModel model − The model that represents the scrollbar's minimum, maximum, extent (aka "visibleAmount") and current value.
protected int orientation
protected int orientation
protected int unitIncrement
protected int unitIncrement
JScrollBar()
Creates a vertical scrollbar with the initial values.
JScrollBar(int orientation)
Creates a scrollbar with the specified orientation and the initial values.
JScrollBar(int orientation, int value, int extent, int min, int max)
Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum.
void addAdjustmentListener(AdjustmentListener l)
Adds an AdjustmentListener.
protected void fireAdjustmentValueChanged(int id, int type, int value)
Notifies listeners that the scrollbar's model has changed.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JScrollBar.
AdjustmentListener[] getAdjustmentListeners()
Returns an array of all the AdjustmentListeners added to this JScrollBar with addAdjustmentListener().
int getBlockIncrement()
For backwards compatibility with java.awt.Scrollbar.
int getBlockIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a block (usually "page") up/down request.
int getMaximum()
The maximum value of the scrollbar is maximum - extent.
Dimension getMaximumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.
int getMinimum()
Returns the minimum value supported by the scrollbar (usually zero).
Dimension getMinimumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.
BoundedRangeModel getModel()
Returns data model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.
int getOrientation()
Returns the component's orientation (horizontal or vertical).
ScrollBarUI getUI()
Returns the delegate that implements the look and feel for this component.
String getUIClassID()
Returns the name of the LookAndFeel class for this component.
int getUnitIncrement()
For backwards compatibility with java.awt.Scrollbar.
int getUnitIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a unit up/down request.
int getValue()
Returns the scrollbar's value.
boolean getValueIsAdjusting()
True if the scrollbar knob is being dragged.
int getVisibleAmount()
Returns the scrollbar's extent, aka its "visibleAmount".
protected String paramString()
Returns a string representation of this JScrollBar.
void removeAdjustmentListener(AdjustmentListener l)
Removes an AdjustmentEvent listener.
void setBlockIncrement(int blockIncrement)
Sets the blockIncrement property.
void setEnabled(boolean x)
Enables the component so that the knob position can be changed.
void setMaximum(int maximum)
Sets the model's maximum property.
void setMinimum(int minimum)
Sets the model's minimum property.
void setModel(BoundedRangeModel newModel)
Sets the model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.
void setOrientation(int orientation)
Set the scrollbar's orientation to either VERTICAL or HORIZONTAL.
void setUI(ScrollBarUI ui)
Sets the L&F object that renders this component.
void setUnitIncrement(int unitIncrement)
Sets the unitIncrement property.
void setValue(int value)
Sets the scrollbar's value.
void setValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property.
void setValues(int newValue, int newExtent, int newMin, int newMax)
Sets the four BoundedRangeModel properties after forcing the arguments to obey the usual constraints.
void setVisibleAmount(int extent)
Set the model's extent property.
void updateUI()
Overrides JComponent.updateUI.
This class inherits methods from the following classes −
java.lang.Object
Create the following Java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >
SwingControlDemo.java
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showScrollbarDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showScrollbarDemo() {                                       
      headerLabel.setText("Control in action: JScrollbar"); 

      final JScrollBar horizontalScroller = new JScrollBar(JScrollBar.HORIZONTAL);
      final JScrollBar verticalScroller = new JScrollBar();
      verticalScroller.setOrientation(JScrollBar.VERTICAL);
      horizontalScroller.setMaximum (100);
      horizontalScroller.setMinimum (1);
      verticalScroller.setMaximum (100);
      verticalScroller.setMinimum (1);
      
      horizontalScroller.addAdjustmentListener(new AdjustmentListener() {
         @Override
         public void adjustmentValueChanged(AdjustmentEvent e) {
            statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "
               + verticalScroller.getValue());
            }
      });
      verticalScroller.addAdjustmentListener(new AdjustmentListener() {
         @Override
         public void adjustmentValueChanged(AdjustmentEvent e) {
            statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "+ verticalScroller.getValue());
         }
      });
      controlPanel.add(horizontalScroller);
      controlPanel.add(verticalScroller);
      
      mainFrame.setVisible(true);  
   } 
}
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output.

    30 Lectures 
     3.5 hours 

 Pranjal Srivastava

    13 Lectures 
     1 hours 

 Pranjal Srivastava

    25 Lectures 
     4.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    14 Lectures 
     1.5 hours 

 Travis Rose

    14 Lectures 
     1 hours 

 Travis Rose
 Print
 Add Notes
 Bookmark this page