Java Video Tutorial 26

Java JSpinnerIn this part of the Java Video Tutorial, I focus on the Java Swing JSpinner component. I’ll show you how to cycle through numbers, dates and just about anything else. I also revisit the Dimension, Date and Calendar libraries.

Most Java Tutorials gloss over topics like this, but I decided I wanted to create a truly comprehensive tutorial.

Just like always it is best to follow the tutorial with the code. It follows the video.

If you like videos like this, share it

Code From the Video

import javax.swing.*;

import java.awt.event.*;

// Object that allows me to use height & width units

import java.awt.Dimension;

// Used to get todays date to use with Calendar

import java.util.Date;

// Editor for JSpinner that allows easy incrementing of dates

import javax.swing.SpinnerDateModel;

// Calendar provides methods that make it easy to work with dates

import java.util.Calendar;

public class Lesson26 extends JFrame{
	JButton button1;
	JSpinner spinner1, spinner2, spinner3, spinner4;
	String outputString = "";
	public static void main(String[] args){
		new Lesson26();
	public Lesson26(){
		this.setTitle("My Fifth Frame");
		JPanel thePanel = new JPanel();
		// Create a button
		button1 = new JButton("Get Answer");
		ListenForButton lForButton = new ListenForButton();
		// Create a basic 1-9 number spinner
		spinner1 = new JSpinner();
		// Create a spinner with initial number, starting number,
		// max number, increment with each click
		spinner2 = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
		// Create a spinner using default values
		String[] weekDays = {"Mon", "Tues", "Weds", "Thurs", "Fri"};
		spinner3 = new JSpinner(new SpinnerListModel(weekDays));
		// Set the size for the spinner so that everything fits
		Dimension d = spinner3.getPreferredSize();
		d.width = 80;
		// Get todays date
		Date todaysDate = new Date();
		// Create a date spinner & set default to today, no minimum, or max
		// Increment the days on button presses
		// Can also increment YEAR, MONTH, or DAY_OF_MONTH
		spinner4 = new JSpinner(new SpinnerDateModel(todaysDate, null, null,
		// DateEditor is an editor that handles displaying & editing the dates
		JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner4, "dd/MM/yy");
		You can add a change listener with Spinners as well
		ListenForSpinner lForSpinner = new ListenForSpinner();
		Tell Java that you want to be alerted when an event
		occurs on the spinner
	private class ListenForButton implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == button1){
				outputString += "Spinner 1 Value: " + spinner1.getValue() + "\n";
				outputString += "Spinner 2 Value: " + spinner2.getValue() + "\n";
				outputString += "Spinner 3 Value: " + spinner3.getValue() + "\n";
				outputString += "Spinner 4 Value: " + spinner4.getValue() + "\n";
				JOptionPane.showMessageDialog(Lesson26.this, outputString, "Information", JOptionPane.INFORMATION_MESSAGE);
				outputString = "";

2 Responses to “Java Video Tutorial 26”

  1. Danijel Vrgoc says:

    Hi, Derek, great tuts you got here, it really helps me learn Java =) However, typing this code from video I noticed that spinner1 won’t stop at 9, but will keep counting up (here’s a link to a screenshot I made I guess the default width doesn’t support displaying more than 1 digit…
    What do you think?

    • admin says:

      Thank you 🙂 This is where you define your jspinner in the code

      // Create a spinner with initial number, starting number,
      // max number, increment with each click

      spinner2 = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));

      I hope that helps

Leave a Reply

Your email address will not be published.