< prev index next >

src/java.desktop/share/classes/javax/swing/JSpinner.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 
  31 import javax.swing.event.*;


  32 import javax.swing.text.*;
  33 import javax.swing.plaf.SpinnerUI;
  34 
  35 import java.util.*;
  36 import java.beans.*;
  37 import java.text.*;
  38 import java.io.*;
  39 import java.text.spi.DateFormatProvider;
  40 import java.text.spi.NumberFormatProvider;
  41 
  42 import javax.accessibility.*;
  43 import sun.util.locale.provider.LocaleProviderAdapter;
  44 import sun.util.locale.provider.LocaleResources;
  45 
  46 /**
  47  * A single line input field that lets the user select a
  48  * number or an object value from an ordered sequence. Spinners typically
  49  * provide a pair of tiny arrow buttons for stepping through the elements
  50  * of the sequence. The keyboard up/down arrow keys also cycle through the
  51  * elements. The user may also be allowed to type a (legal) value directly


 726          * This class ignores <code>PropertyChangeEvents</code> whose
 727          * source is not the <code>JFormattedTextField</code>, so subclasses
 728          * may safely make <code>this</code> <code>DefaultEditor</code> a
 729          * <code>PropertyChangeListener</code> on other objects.
 730          *
 731          * @param e the <code>PropertyChangeEvent</code> whose source is
 732          *    the <code>JFormattedTextField</code> created by this class.
 733          * @see #getTextField
 734          */
 735         public void propertyChange(PropertyChangeEvent e)
 736         {
 737             JSpinner spinner = getSpinner();
 738 
 739             if (spinner == null) {
 740                 // Indicates we aren't installed anywhere.
 741                 return;
 742             }
 743 
 744             Object source = e.getSource();
 745             String name = e.getPropertyName();
 746             if ((source instanceof JFormattedTextField) && "value".equals(name)) {

 747                 Object lastValue = spinner.getValue();
 748 
 749                 // Try to set the new value
 750                 try {
 751                     spinner.setValue(getTextField().getValue());
 752                 } catch (IllegalArgumentException iae) {
 753                     // SpinnerModel didn't like new value, reset
 754                     try {
 755                         ((JFormattedTextField)source).setValue(lastValue);
 756                     } catch (IllegalArgumentException iae2) {
 757                         // Still bogus, nothing else we can do, the
 758                         // SpinnerModel and JFormattedTextField are now out
 759                         // of sync.
 760                     }
 761                 }











 762             }
 763         }
 764 
 765 
 766         /**
 767          * This <code>LayoutManager</code> method does nothing.  We're
 768          * only managing a single child and there's no support
 769          * for layout constraints.
 770          *
 771          * @param name ignored
 772          * @param child ignored
 773          */
 774         public void addLayoutComponent(String name, Component child) {
 775         }
 776 
 777 
 778         /**
 779          * This <code>LayoutManager</code> method does nothing.  There
 780          * isn't any per-child state.
 781          *


   1 /*
   2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 
  31 import javax.swing.event.*;
  32 import javax.swing.plaf.FontUIResource;
  33 import javax.swing.plaf.UIResource;
  34 import javax.swing.text.*;
  35 import javax.swing.plaf.SpinnerUI;
  36 
  37 import java.util.*;
  38 import java.beans.*;
  39 import java.text.*;
  40 import java.io.*;
  41 import java.text.spi.DateFormatProvider;
  42 import java.text.spi.NumberFormatProvider;
  43 
  44 import javax.accessibility.*;
  45 import sun.util.locale.provider.LocaleProviderAdapter;
  46 import sun.util.locale.provider.LocaleResources;
  47 
  48 /**
  49  * A single line input field that lets the user select a
  50  * number or an object value from an ordered sequence. Spinners typically
  51  * provide a pair of tiny arrow buttons for stepping through the elements
  52  * of the sequence. The keyboard up/down arrow keys also cycle through the
  53  * elements. The user may also be allowed to type a (legal) value directly


 728          * This class ignores <code>PropertyChangeEvents</code> whose
 729          * source is not the <code>JFormattedTextField</code>, so subclasses
 730          * may safely make <code>this</code> <code>DefaultEditor</code> a
 731          * <code>PropertyChangeListener</code> on other objects.
 732          *
 733          * @param e the <code>PropertyChangeEvent</code> whose source is
 734          *    the <code>JFormattedTextField</code> created by this class.
 735          * @see #getTextField
 736          */
 737         public void propertyChange(PropertyChangeEvent e)
 738         {
 739             JSpinner spinner = getSpinner();
 740 
 741             if (spinner == null) {
 742                 // Indicates we aren't installed anywhere.
 743                 return;
 744             }
 745 
 746             Object source = e.getSource();
 747             String name = e.getPropertyName();
 748             if (source instanceof JFormattedTextField) {
 749                 if ("value".equals(name)) {
 750                     Object lastValue = spinner.getValue();
 751 
 752                     // Try to set the new value
 753                     try {
 754                         spinner.setValue(getTextField().getValue());
 755                     } catch (IllegalArgumentException iae) {
 756                         // SpinnerModel didn't like new value, reset
 757                         try {
 758                             ((JFormattedTextField)source).setValue(lastValue);
 759                         } catch (IllegalArgumentException iae2) {
 760                             // Still bogus, nothing else we can do, the
 761                             // SpinnerModel and JFormattedTextField are now out
 762                             // of sync.
 763                         }
 764                     }
 765                 } else if ("font".equals(name)) {
 766                     Object newfont = e.getNewValue();
 767                     if (newfont instanceof UIResource) {
 768                         // The text field font must match the JSpinner font if
 769                         // the text field font was not set by the user
 770                         Font font = spinner.getFont();
 771                         if (!newfont.equals(font)) {
 772                             getTextField().setFont(new FontUIResource(font));
 773                         }
 774                     }
 775                 }
 776             }
 777         }
 778 
 779 
 780         /**
 781          * This <code>LayoutManager</code> method does nothing.  We're
 782          * only managing a single child and there's no support
 783          * for layout constraints.
 784          *
 785          * @param name ignored
 786          * @param child ignored
 787          */
 788         public void addLayoutComponent(String name, Component child) {
 789         }
 790 
 791 
 792         /**
 793          * This <code>LayoutManager</code> method does nothing.  There
 794          * isn't any per-child state.
 795          *


< prev index next >