ItsSpirit
Fun Creator
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
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
Download
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 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.
- vbar.setOrientation
(
JScrollBar
.VERTICAL
)
;
To set the minimum and maximum value of our scrollbars, we will use the setMinimum and setMaximum method.
- hbar.setMaximum
(
200
)
;
- hbar.setMinimum
(
1
)
;
- vbar.setMaximum
(
200
)
;
- 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.
- 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. 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:
- frame.getContentPane
(
)
.add
(
hbar, "South"
)
;
- frame.getContentPane
(
)
.add
(
vbar, "East"
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.setSize
(
300
, 200
)
;
- frame.setVisible
(
true
)
;
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
)
;
- }
- }
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
Download
You must upgrade your account or reply in the thread to view hidden text.