JScrollBar Component in Java

This is a tutorial in which we will going to create 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:
  1. import java.awt.event.*; // used to access AdjustmentEvent and AdjustmentListener class
  2. 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.
  1.     JFrame frame = new JFrame("JScrollBar Component");
  2.     final JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL);
  3.     final JScrollBar vbar = new JScrollBar();
As you have seen the code above, I declared the scrollbar to be final because i will create 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.
  1.         vbar.setOrientation(JScrollBar.VERTICAL);
To set the minimum and maximum value of our scrollbars, we will use the setMinimum and setMaximum method.
  1.     hbar.setMaximum(200);
  2.       hbar.setMinimum (1);
  3.       vbar.setMaximum (200);
  4.       vbar.setMinimum (1);
4. Now, to have the current value of the scrollbars, we will have to create the AdjustmentEvent and AdjustmentListener of the hbar and vbar variables as we have the getValue method.
  1.    hbar.addAdjustmentListener(new AdjustmentListener() {
  2.          public void adjustmentValueChanged(AdjustmentEvent e) {
  3.             System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
  4.             }
  5.       });
  6.      
  7.          vbar.addAdjustmentListener(new AdjustmentListener() {
  8.          public void adjustmentValueChanged(AdjustmentEvent e) {
  9.             System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
  10.             }
  11.       });
5. Now, add the hbar variable to the frame using the default BorderLayout of South position as we will put this in the bottom and vbar variable in the East for its right position of the getContentPane method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1.     frame.getContentPane().add(hbar, "South");
  2.     frame.getContentPane().add(vbar, "East");
  3.  
  4.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5.     frame.setSize(300, 200);
  6.     frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.event.*; // used to access AdjustmentEvent and AdjustmentListener class
  2. import javax.swing.*; //used to access the JFrame and JScrollBar class
  3.  
  4. public class jScrollBarComponent {
  5.  
  6.  
  7.   public static void main(String s[]) {
  8.     JFrame frame = new JFrame("JScrollBar Component");
  9.    
  10.     final JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL);
  11.     final JScrollBar vbar = new JScrollBar();
  12.         vbar.setOrientation(JScrollBar.VERTICAL);
  13.      hbar.setMaximum(200);
  14.       hbar.setMinimum (1);
  15.       vbar.setMaximum (200);
  16.       vbar.setMinimum (1);
  17.  
  18.    hbar.addAdjustmentListener(new AdjustmentListener() {
  19.          public void adjustmentValueChanged(AdjustmentEvent e) {
  20.             System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
  21.             }
  22.       });
  23.      
  24.          vbar.addAdjustmentListener(new AdjustmentListener() {
  25.          public void adjustmentValueChanged(AdjustmentEvent e) {
  26.             System.out.println("Horizontal: " +hbar.getValue() + " Vertical: " + vbar.getValue());
  27.             }
  28.       });
  29.  
  30.     frame.getContentPane().add(hbar, "South");
  31.     frame.getContentPane().add(vbar, "East");
  32.  
  33.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.     frame.setSize(300, 200);
  35.     frame.setVisible(true);
  36.   }
  37. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment