JScrollBar Component in Java

May 28, 2020
Tutorials
jscrollbar

This is a tutorial wherein we are going to make a program that will have a JScrollBar Component in Java. A JScrollBar is used to let the user have a scrollbar in other components.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jScrollBarComponent.java.

2. Import the following packages:

import java.awt.event.*; // used to access AdjustmentEvent and AdjustmentListener class
import javax.swing.*; //used to access the JFrame and JScrollBar class

3. Initialize your variable in your Main, variable frame for JFrame, variable hbar and vbar for JScrollBar component.

JFrame frame = new JFrame("JScrollBar Component");
final JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL);
final JScrollBar vbar = new JScrollBar();

As you notice, I declared the scrollbar to be final because I will make an inner class of it to have the AdjustmentEvent and AdjustmentListener class. The hbar variable declared to have an orientation of a horizontal scrollbar as we have JScrollBar.HORIZONTAL.

To declare vbar to be a vertical srcollbar, we will use the setOrientation method.

vbar.setOrientation(JScrollBar.VERTICAL);

To set the minimum and maximum value of our scrollbars, we will be going to use the setMinimum and setMaximum method.

hbar.setMaximum(200);
  hbar.setMinimum (1);
  vbar.setMaximum (200);
  vbar.setMinimum (1);

4. To have the current value of the scrollbars, we have to create the AdjustmentEvent and AdjustmentListener of the hbar and vbar variables as we have the getValue method.

hbar.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
         System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
         }
   });
 
      vbar.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
         System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
         }
   });

5. Add the hbar variable to the frame using the default BorderLayout of South position as we put this in the bottom and vbar variable in the East for its right position of the getContentPane method. Finally, set the size, visibility, and the close operation of the frame. Copy this code below:

frame.getContentPane().add(hbar, "South");
frame.getContentPane().add(vbar, "East");
 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);

Output:

output


Here’s the full code of this tutorial:

import java.awt.event.*; // used to access AdjustmentEvent and AdjustmentListener class
import javax.swing.*; //used to access the JFrame and JScrollBar class
 
public class jScrollBarComponent {
 
 
  public static void main(String s[]) {
    JFrame frame = new JFrame("JScrollBar Component");
 
    final JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL);
    final JScrollBar vbar = new JScrollBar();
   	vbar.setOrientation(JScrollBar.VERTICAL);
     hbar.setMaximum(200);
      hbar.setMinimum (1);
      vbar.setMaximum (200);
      vbar.setMinimum (1);
 
   hbar.addAdjustmentListener(new AdjustmentListener() {
         public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
            }
      });
 
         vbar.addAdjustmentListener(new AdjustmentListener() {
         public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
            }
      });
 
    frame.getContentPane().add(hbar, "South");
    frame.getContentPane().add(vbar, "East");
 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.