How do I create a JSpinner component?

JSpinner is a single line input field with two buttons (arrow up and arrow down) that allow us to select a value like number or object from a sequence value. It looks like a combobox without a drop-down.

In the following example we create the default JSpinner that will give us a spinner to select an integer value from it.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class JSpinnerCreate extends JFrame {
    public JSpinnerCreate() {

    public static void main(String[] args) {
                () -> new JSpinnerCreate().setVisible(true));

    private void initialize() {
        setSize(500, 500);
        setLayout(new BorderLayout());

        // Create an instance of JSpinner and put it at the top of the frame.
        final JSpinner spinner = new JSpinner();
        getContentPane().add(spinner, BorderLayout.NORTH);

        // Create a JButton and print out the value of the JSpinner when
        // the button is clicked.
        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> {
            Integer value = (Integer) spinner.getValue();
            System.out.println("value = " + value);
        getContentPane().add(okButton, BorderLayout.SOUTH);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.