Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Updating JSpinner question

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Default Updating JSpinner question

    Hello everyone. I have a running program that uses 4 JSpinners first one include complete day,month,year and the three spinners are day, month, year. the program now when I play with the bottom three spinners, it updates the first spinner, the one that contains complete date. What I want to do is when I change day or month or year in the first spinner, I want the program to update the correct spinner

    Here is the code I have so far.
    Thanks in advance

     import javax.swing.*;
       import javax.swing.event.*;
       import java.util.*;
       import java.text.*;
       import java.awt.*;
       public class Spinner extends JFrame {
       // Create four spinners for date, day, month, and year
          private JSpinner jspDate = new JSpinner(new SpinnerDateModel());
          private JSpinner jspDay = new JSpinner(new SpinnerNumberModel(1, 1, 31, 1));
          private String[] monthNames = new DateFormatSymbols().getMonths();
          private JSpinner jspMonth = new JSpinner (new SpinnerListModel(Arrays.asList(monthNames).subList(0, 12)));
          private JSpinner spinnerYear = new JSpinner(new SpinnerNumberModel(2004, 1, 3000, 1));
          public Spinner() 
          // Group labels
             JPanel panel1 = new JPanel();
             panel1.setLayout(new GridLayout(4, 1));
             panel1.add(new JLabel("Date"));
             panel1.add(new JLabel("Day"));
             panel1.add(new JLabel("Month"));
             panel1.add(new JLabel("Year"));
          // Group spinners
             JPanel panel2 = new JPanel();
             panel2.setLayout(new GridLayout(4, 1));
          // Add spinner and label to the UI
             add(panel1, BorderLayout.WEST);
             add(panel2, BorderLayout.CENTER);
          // Set editor for date
             JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(jspDate, "MMM dd, yyyy");
          // Set editor for year
             JSpinner.NumberEditor yearEditor = new JSpinner.NumberEditor(spinnerYear, "####");
          // Update date to synchronize with the day, month, and year
          // Register and create a listener for jspDay
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
          // Register and create a listener for jspMonth
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
          // Register and create a listener for spinnerYear
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
       /** Update date spinner to synchronize with the other spinners */
          private void updateDate() 
          // Get current month and year in int
             int month = ((SpinnerListModel)jspMonth.getModel()).getList().indexOf(jspMonth.getValue());
             int year = ((Integer)spinnerYear.getValue()).intValue();
          // Set a new maximum number of days for the new month and year
             SpinnerNumberModel numberModel = (SpinnerNumberModel)jspDay.getModel();
             numberModel.setMaximum(new Integer(maxDaysInMonth(year, month)));
          // Set a new current day if it exceeds the maximum
             if (((Integer)(numberModel.getValue())).intValue() > maxDaysInMonth(year, month))
                numberModel.setValue(new Integer(maxDaysInMonth(year, month)));
          // Get the current day
             int day = ((Integer)jspDay.getValue()).intValue();
          // Set a new date in the date spinner
             jspDate.setValue(new GregorianCalendar(year, month, day).getTime());
    			System.out.println(">> " + jspDate.getValue());
    			//System.out.println(">> " + jspDate.getValue("year"));
       /** Return the maximum number of days in a month. For example,
        Feb 2004 has 29 days. */
          private int maxDaysInMonth(int year, int month) 
             Calendar calendar = new GregorianCalendar(year, month, 1);
             return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
          public static void main(String[] args) 
             Spinner frame = new Spinner();
          // Display the frame

  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Fort Wayne, Indiana
    Thanked 31 Times in 31 Posts

    Default Re: Updating JSpinner question

    It looks like you're really close. You just need to add a similar change listener to the date spinner. In that listener, parse the date and update the other spinners accordingly. You can either determine what changed and just change that spinner or you could just update all three since it wont make a lot of difference in the speed of the program. Then make sure that you arent going to get into a continuous loop of spinners updating each other.

    Just checked it out, updating the value of a spinner wont trigger its change event so you dont have to worry about the continuous loop.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    On vacation
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Updating JSpinner question

    What have you tried? I don't see a listener for the first spinner.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. n00b asking jspinner question
    By john123 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: March 25th, 2012, 07:00 AM
  2. Replies: 0
    Last Post: February 20th, 2012, 02:04 PM
  3. Updating JList
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 3
    Last Post: October 6th, 2011, 07:17 AM
  4. Remove focus from JSpinner?
    By hafunui in forum AWT / Java Swing
    Replies: 4
    Last Post: June 16th, 2011, 04:17 PM
  5. Help with JSpinner Problem
    By leoeroz in forum AWT / Java Swing
    Replies: 0
    Last Post: December 25th, 2010, 12:42 PM