JScrollBar Component in Java
Submitted by donbermoy on Monday, November 24, 2014 - 13:33.
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:
3. Initialize your variable in your Main, variable frame for JFrame, variable hbar and vbar for JScrollBar component.
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.
To set the minimum and maximum value of our scrollbars, we will use the setMinimum and setMaximum method.
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.
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:
Output:
Here's the full code of this tutorial:
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
- import java.awt.event.*; // used to access AdjustmentEvent and AdjustmentListener class
- import javax.swing.*; //used to access the JFrame and JScrollBar class
- hbar.setMaximum(200);
- hbar.setMinimum (1);
- vbar.setMaximum (200);
- vbar.setMinimum (1);
- }
- });
- }
- });
- frame.getContentPane().add(hbar, "South");
- frame.getContentPane().add(vbar, "East");
- frame.setSize(300, 200);
- frame.setVisible(true);
- 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 {
- hbar.setMaximum(200);
- hbar.setMinimum (1);
- vbar.setMaximum (200);
- vbar.setMinimum (1);
- }
- });
- }
- });
- frame.getContentPane().add(hbar, "South");
- frame.getContentPane().add(vbar, "East");
- frame.setSize(300, 200);
- frame.setVisible(true);
- }
- }
Add new comment
- 28 views