Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4467978

JSpinner.setEditor fails to update the date editor properly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • client-libs
    • beta2
    • x86
    • windows_nt



      Name: yyT116575 Date: 06/08/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      /*
       * run application and use radio buttons to change date editor format
       * which fails to update properly.
       */
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import java.util.*;

      /**
       * An application that displays date/time in various formats
       * allowing the user to modify the date/time (initially current date/time)
       */
      public class DateSpin extends JPanel
      {
        static JFrame frame;

        final static String yearMonthnameDayStr = "Year|Monthname|Day";
        final static String yearMonthDayStr = "Year|Month|Day";
        final static String yearJdayStr = "Year|Jday";
        final static String yearJdayTightStr = "Year|JdayTight";
        SpinnerDateModel dateModel;
        JSpinner dateSelector;
        JSpinner.DateEditor initialDateEditor = null;
        
        JRadioButton yearMonthnameDayButton;
        JRadioButton yearMonthDayButton;
        JRadioButton yearJdayButton;
        JRadioButton yearJdayTightButton;
        
        public DateSpin()
        {
          Calendar today = Calendar.getInstance();
          Date currDate = today.getTime();
          Calendar start = Calendar.getInstance();
          start.add(Calendar.DAY_OF_YEAR, -3);
          Date startDate = start.getTime();
          Calendar end = Calendar.getInstance();
          end.add(Calendar.DAY_OF_YEAR, 3);
          Date endDate = end.getTime();
          dateModel = new SpinnerDateModel(currDate, startDate, endDate,
      Calendar.DAY_OF_YEAR);
          ////dateModel = new SpinnerDateModel();
          dateSelector = new JSpinner(dateModel);

          // Create the Date & Time User Interface
          JLabel label = new JLabel("Start Date:");
          
          yearMonthnameDayButton = new JRadioButton(yearMonthnameDayStr);
          yearMonthnameDayButton.setMnemonic('d');
          yearMonthnameDayButton.setToolTipText("Year Monthname Day");
          yearMonthnameDayButton.setActionCommand(yearMonthnameDayStr);
          yearMonthnameDayButton.setEnabled(true);
          
          yearMonthDayButton = new JRadioButton(yearMonthDayStr);
          yearMonthDayButton.setMnemonic('a');
          yearMonthDayButton.setToolTipText("Year Month Day");
          yearMonthDayButton.setActionCommand(yearMonthDayStr);
          yearMonthDayButton.setEnabled(true);
          
          yearJdayButton = new JRadioButton(yearJdayStr);
          yearJdayButton.setMnemonic('j');
          yearJdayButton.setToolTipText("Year Jday");
          yearJdayButton.setActionCommand(yearJdayStr);
          yearJdayButton.setEnabled(true);
          
          yearJdayButton.setSelected(true);
          initialDateEditor = new JSpinner.DateEditor(dateSelector, "yyyy DDD HH:mm:ss");
          dateSelector.setEditor(initialDateEditor);

          yearJdayTightButton = new JRadioButton(yearJdayTightStr);
          yearJdayTightButton.setMnemonic('t');
          yearJdayTightButton.setToolTipText("Year Jday tight");
          yearJdayTightButton.setActionCommand(yearJdayTightStr);
          yearJdayTightButton.setEnabled(true);
          
          // Group the radio buttons.
          ButtonGroup group = new ButtonGroup();
          group.add(yearMonthnameDayButton);
          group.add(yearMonthDayButton);
          group.add(yearJdayButton);
          group.add(yearJdayTightButton);
          
          // Register a listener for the radio buttons.
          RadioListener myListener = new RadioListener();
          yearMonthnameDayButton.addActionListener(myListener);
          yearJdayButton.addActionListener(myListener);
          yearMonthDayButton.addActionListener(myListener);
          
          add(label);
          add(dateSelector);
          add(yearMonthnameDayButton);
          add(yearMonthDayButton);
          add(yearJdayButton);
          add(yearJdayTightButton);
        }
        
        
        /** An ActionListener that listens to the radio buttons. */
        class RadioListener implements ActionListener
        {
          public void actionPerformed(ActionEvent event)
          {
            String action = event.getActionCommand();

            try
            {
              java.util.Date dateSelected = dateModel.getDate();
              System.out.println(dateSelected.toString());

              JSpinner.DateEditor editor = null;
              if (action.equals(yearMonthnameDayStr))
              {
                editor = new JSpinner.DateEditor(dateSelector, "yyyy MMM dd HH:mm:ss");
              }
              else if (action.equals(yearMonthDayStr))
              {
                editor = new JSpinner.DateEditor(dateSelector, "yyyy MM dd HH:mm:ss");
              }
              else if (action.equals(yearJdayStr))
              {
                editor = new JSpinner.DateEditor(dateSelector, "yyyy DDD HH:mm:ss");
              }
              else if (action.equals(yearJdayTightStr))
              {
                editor = new JSpinner.DateEditor(dateSelector, "yyyyDDDHHmmss");
              }
              else
                System.out.println("Unknown date format: " + action.toString());
                
              if (editor != null)
                dateSelector.setEditor(editor);
              else
                System.out.println ("Date editor is null!");
              
              frame.pack();
            }
            catch (Exception exc)
            {
              JRadioButton button = (JRadioButton)event.getSource();
              button.setEnabled(false);
            }
          }
        }
        
        public static void main(String s[])
        {
          /*
          try
          {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch (Exception exc)
          {
            System.err.println("Error loading System Look & Feel: " + exc);
          }
          */
          DateSpin panel = new DateSpin();
          
          frame = new JFrame("Date & Time");
          frame.addWindowListener(new WindowAdapter()
             {public void windowClosing(WindowEvent e) {System.exit(0);}});
          frame.getContentPane().add("Center", panel);
          frame.pack();
          frame.setVisible(true);
        }
      }
      (Review ID: 126192)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: