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

JSpinner Component in Java

tim09leary

Professional Procrastinator
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
150
Likes
38
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This is a tutorial in which we will going to create a program that will have a JSpinner Component in Java.

A JSpinner Component is similar to a JComboBox and JList in java that it has object options to choose but this component has only a single line input field that lets the user select an object value from an ordered sequence.

It will have the SpinnerModel to have the JSpinner's sequence.

So, now let's start this tutorial!

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

2. Import the following packages:
  1. import

    java.awt.*

    ;

    //used to access the flowlayout
  2. import

    javax.swing.*

    ;

    // used to access the JSpinner, SpinnerListModel, SpinnerModel, and the JFrame Class

3. Initialize your variable in your Main, variable months for the String array, variable spinModel for SpinnerModel class, variable spinner for JSpinner component, and variable frame for the JFrame class.

  1. String

    months[

    ]

    =

    {

    "January"

    ,"February"

    ,"March"

    ,"April"

    ,"May"

    ,"June"

    ,"July"

    ,"August"

    ,"September"

    ,"October"

    ,"November"

    ,"December"

    }

    ;
  2. SpinnerModel spinModel =

    new

    SpinnerListModel(

    months)

    ;
  3. JSpinner spinner =

    new

    JSpinner(

    spinModel)

    ;
  4. JFrame

    frame =

    new

    JFrame

    (

    "JSpinner Component"

    )

    ;

We have created a String array of months to display these elements in the spinner. But before putting the array value to the JSpinner, we will put it first to the SpinnerModel class and then put the spinModel variable of this class to the JSpinner.

Then we have the JFrame component that will serve as the container of the objects and has the flowlayout as the layout used as we have this code below:

  1. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  2. frame.getContentPane

    (

    )

    .add

    (

    spinner)

    ;

4. Lastly, set the size and visibility of the Frame.

  1. frame.setSize

    (

    300

    , 100

    )

    ;
  2. frame.setVisible

    (

    true

    )

    ;


Output:

jspinner.png


Here's the full code of this tutorial:

  1. import

    java.awt.*

    ;

    //used to access the flowlayout
  2. import

    javax.swing.*

    ;

    // used to access the JSpinner, SpinnerListModel, SpinnerModel, and the JFrame Class


  3. public

    class

    jSpinnerComponent {
  4. public

    static

    void

    main(

    String

    args[

    ]

    )

    throws

    Exception

    {
  5. String

    months[

    ]

    =

    {

    "January"

    ,"February"

    ,"March"

    ,"April"

    ,"May"

    ,"June"

    ,"July"

    ,"August"

    ,"September"

    ,"October"

    ,"November"

    ,"December"

    }

    ;
  6. SpinnerModel spinModel =

    new

    SpinnerListModel(

    months)

    ;
  7. JSpinner spinner =

    new

    JSpinner(

    spinModel)

    ;
  8. JFrame

    frame =

    new

    JFrame

    (

    "JSpinner Component"

    )

    ;



  9. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    FlowLayout

    (

    )

    )

    ;
  10. frame.getContentPane

    (

    )

    .add

    (

    spinner)

    ;


  11. frame.setSize

    (

    300

    , 100

    )

    ;
  12. frame.setVisible

    (

    true

    )

    ;


  13. }
  14. }

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
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.
 

450,270

322,965

322,974

Top