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.


>> REGISTER NOW TO START POSTING


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
    Posts
    1
    Thanks
    0
    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));
             panel2.add(jspDate);
             panel2.add(jspDay);
             panel2.add(jspMonth);
             panel2.add(spinnerYear);
     
          // 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");
             jspDate.setEditor(dateEditor);
     
          // Set editor for year
             JSpinner.NumberEditor yearEditor = new JSpinner.NumberEditor(spinnerYear, "####");
             spinnerYear.setEditor(yearEditor);
     
          // Update date to synchronize with the day, month, and year
             updateDate();
     
          // Register and create a listener for jspDay
             jspDay.addChangeListener(
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
                         updateDate();
                      }
                   });
     
          // Register and create a listener for jspMonth
             jspMonth.addChangeListener(
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
                         updateDate();
                      }
                   });
     
          // Register and create a listener for spinnerYear
             spinnerYear.addChangeListener(
                   new ChangeListener() {
                      public void stateChanged(javax.swing.event.ChangeEvent e) {
                         updateDate();
                      }
                   });
          }
     
       /** 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"));
    			//jspDay.setValue(jspDate.getValue());
          }
     
     
       /** 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();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // Display the frame
             frame.setSize(400,200);
             frame.setVisible(true);
          }
       }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    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.

    ---Update---
    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
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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