JSpinner Component in Java

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 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.  
  4.  
  5. public class jSpinnerComponent {
  6. public static void main(String args[]) throws Exception {
  7. String months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  8. SpinnerModel spinModel = new SpinnerListModel(months);
  9. JSpinner spinner = new JSpinner(spinModel);
  10. JFrame frame = new JFrame("JSpinner Component");
  11.  
  12.  
  13.  
  14. frame.getContentPane().setLayout(new FlowLayout());
  15. frame.getContentPane().add(spinner);
  16.  
  17.  
  18. frame.setSize(300, 100);
  19. frame.setVisible(true);
  20.  
  21.  
  22. }
  23. }
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

Add new comment