• We just launched and are currently in beta. Join us as we build and grow the community.

JScrollBar Component in Java

ItsSpirit

Fun Creator
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
143
Likes
192
Bits
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:
  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. vbar.addAdjustmentListener

    (

    new

    AdjustmentListener

    (

    )

    {
  7. public

    void

    adjustmentValueChanged(

    AdjustmentEvent

    e)

    {
  8. System

    .out

    .println

    (

    "Horizontal: "

    +

    hbar.getValue

    (

    )

    +

    " Vertical: "

    +

    vbar.getValue

    (

    )

    )

    ;
  9. }
  10. }

    )

    ;

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. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  4. frame.setSize

    (

    300

    , 200

    )

    ;
  5. frame.setVisible

    (

    true

    )

    ;


Output:

jscrollbar.png


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. public

    class

    jScrollBarComponent {


  4. public

    static

    void

    main(

    String

    s[

    ]

    )

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "JScrollBar Component"

    )

    ;

  6. final

    JScrollBar

    hbar =

    new

    JScrollBar

    (

    JScrollBar

    .HORIZONTAL

    )

    ;
  7. final

    JScrollBar

    vbar =

    new

    JScrollBar

    (

    )

    ;
  8. vbar.setOrientation

    (

    JScrollBar

    .VERTICAL

    )

    ;
  9. hbar.setMaximum

    (

    200

    )

    ;
  10. hbar.setMinimum

    (

    1

    )

    ;
  11. vbar.setMaximum

    (

    200

    )

    ;
  12. vbar.setMinimum

    (

    1

    )

    ;

  13. hbar.addAdjustmentListener

    (

    new

    AdjustmentListener

    (

    )

    {
  14. public

    void

    adjustmentValueChanged(

    AdjustmentEvent

    e)

    {
  15. System

    .out

    .println

    (

    "Horizontal: "

    +

    hbar.getValue

    (

    )

    +

    " Vertical: "

    +

    vbar.getValue

    (

    )

    )

    ;
  16. }
  17. }

    )

    ;

  18. vbar.addAdjustmentListener

    (

    new

    AdjustmentListener

    (

    )

    {
  19. public

    void

    adjustmentValueChanged(

    AdjustmentEvent

    e)

    {
  20. System

    .out

    .println

    (

    "Horizontal: "

    +

    hbar.getValue

    (

    )

    +

    " Vertical: "

    +

    vbar.getValue

    (

    )

    )

    ;
  21. }
  22. }

    )

    ;

  23. frame.getContentPane

    (

    )

    .add

    (

    hbar, "South"

    )

    ;
  24. frame.getContentPane

    (

    )

    .add

    (

    vbar, "East"

    )

    ;

  25. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  26. frame.setSize

    (

    300

    , 200

    )

    ;
  27. frame.setVisible

    (

    true

    )

    ;
  28. }
  29. }

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.
 

452,292

323,526

323,535

Top