Time Spinner OverwriteMode JAVA

问题描述:

我已经使用了我在互联网上找到的这段代码,我只想知道为什么只有高亮显示时使用键盘输入才能覆盖微调器的“秒”部分。使用的时间格式是HH:mm:ss。Time Spinner OverwriteMode JAVA

如果“小时”或“分钟”部分突出显示,并且我按下数字键盘上的数字,则什么也不会发生。我在这里错过了什么吗?

由于

import javax.swing.*; 
import javax.swing.text.DefaultFormatter; 
import javax.swing.text.InternationalFormatter; 
import java.text.DateFormat; 
import java.text.Format; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

/** 
* <code>DateSpinner</code> is a spinner that is specialized in displaying or editing a a date or time. 
* <p/> 
* To change the value, you can use {@link #setValue(Object)} and pass in a Date. To get the 
* Date, using {@link #getValue()}. 
*/ 
public class DateSpinner extends JSpinner { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    public DefaultFormatter _formatter; 
    public DateEditor _timeEditor; 
    public DateFormat _format; 

    /** 
    * Creates a date spinner using "hh:mm:ss" as the format string. 
    */ 
    public DateSpinner() { 
     this("hh:mm:ss"); 
    } 

    /** 
    * Creates a date spinner using the specified format string. 
    * 
    * @param format the format string as defined in {@link java.text.SimpleDateFormat}. 
    */ 
    public DateSpinner(String format) { 
     this(format, new Date()); 
    } 

    /** 
    * Creates a date spinner using the specified format string and an initial value. 
    * 
    * @param format the format string as defined in {@link java.text.SimpleDateFormat}. 
    * @param date initial value 
    */ 
    public DateSpinner(String format, Date date) { 
     super(new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY)); 

     setFormat(format); 

     customizeSpinner(); 
    } 

    private void customizeDateEditor() { 
     JFormattedTextField.AbstractFormatter formatter = _timeEditor.getTextField().getFormatter(); 
     if (formatter instanceof DefaultFormatter) { 
      _formatter = (DefaultFormatter) formatter; 
     } 
     else { 
      throw new IllegalStateException("The formatter is not an instance of DefaultFormatter."); 
     } 

     if (formatter instanceof InternationalFormatter) { 
      Format f = ((InternationalFormatter) formatter).getFormat(); 
      if (f instanceof DateFormat) { 
       _format = ((DateFormat) f); 
      } 
     } 

     if (_format == null) { 
      throw new IllegalStateException("The format is not an instance of SimpleDateFormat."); 
     } 
    } 

    /** 
    * Sets the date format string used by this DateSpinner. Please note, this method call 
    * will recreate the DateEditor used by DateSpinner. 
    * 
    * @param format 
    */ 
    public void setFormat(String format) { 
     _timeEditor = createDateEditor(format); 
     customizeDateEditor(); 
     setEditor(_timeEditor); 
    } 

    /** 
    * Customizes the spinner. 
    */ 
    protected void customizeSpinner() { 
     setLenient(false); 
     setCommitsOnValidEdit(true); 
     setAllowsInvalid(false); 
     setOverwriteMode(true); 
     //SpinnerWheelSupport.installMouseWheelSupport(this); 
    } 

    /** 
    * Creates the DateEditor. 
    * 
    * @param format 
    * @return the DateEditor. 
    */ 
    protected DateEditor createDateEditor(String format) { 
     return new DateEditor(this, format); 
    } 

    /** 
    * Sets when edits are published back to the 
    * <code>JFormattedTextField</code>. If true, <code>commitEdit</code> 
    * is invoked after every valid edit (any time the text is edited). On 
    * the other hand, if this is false than the <code>DefaultFormatter</code> 
    * does not publish edits back to the <code>JFormattedTextField</code>. 
    * As such, the only time the value of the <code>JFormattedTextField</code> 
    * will change is when <code>commitEdit</code> is invoked on 
    * <code>JFormattedTextField</code>, typically when enter is pressed 
    * or focus leaves the <code>JFormattedTextField</code>. 
    * 
    * @param commit Used to indicate when edits are committed back to the 
    *    JTextComponent 
    */ 
    public void setCommitsOnValidEdit(boolean commit) { 
     _formatter.setCommitsOnValidEdit(commit); 
    } 

    /** 
    * Returns when edits are published back to the 
    * <code>JFormattedTextField</code>. 
    * 
    * @return true if edits are committed after every valid edit 
    */ 
    public boolean getCommitsOnValidEdit() { 
     return _formatter.getCommitsOnValidEdit(); 
    } 

    /** 
    * Configures the behavior when inserting characters. If 
    * <code>overwriteMode</code> is true (the default), new characters 
    * overwrite existing characters in the model. 
    * 
    * @param overwriteMode Indicates if overwrite or overstrike mode is used 
    */ 
    public void setOverwriteMode(boolean overwriteMode) { 
     _formatter.setOverwriteMode(overwriteMode); 
    } 

    /** 
    * Returns the behavior when inserting characters. 
    * 
    * @return true if newly inserted characters overwrite existing characters 
    */ 
    public boolean getOverwriteMode() { 
     return _formatter.getOverwriteMode(); 
    } 

    /** 
    * Sets whether or not the value being edited is allowed to be invalid 
    * for a length of time (that is, <code>stringToValue</code> throws 
    * a <code>ParseException</code>). 
    * It is often convenient to allow the user to temporarily input an 
    * invalid value. 
    * 
    * @param allowsInvalid Used to indicate if the edited value must always 
    *      be valid 
    */ 
    public void setAllowsInvalid(boolean allowsInvalid) { 
     _formatter.setAllowsInvalid(allowsInvalid); 
    } 

    /** 
    * Returns whether or not the value being edited is allowed to be invalid 
    * for a length of time. 
    * 
    * @return false if the edited value must always be valid 
    */ 
    public boolean getAllowsInvalid() { 
     return _formatter.getAllowsInvalid(); 
    } 

    /** 
    * Sets the time zone for the calendar of this DateFormat object. 
    * 
    * @param zone the given new time zone. 
    */ 
    public void setTimeZone(TimeZone zone) { 
     _format.setTimeZone(zone); 
    } 

    /** 
    * Gets the time zone. 
    * 
    * @return the time zone associated with the calendar of DateFormat. 
    */ 
    public TimeZone getTimeZone() { 
     return _format.getTimeZone(); 
    } 

    /** 
    * Specify whether or not date/time parsing is to be lenient. With 
    * lenient parsing, the parser may use heuristics to interpret inputs that 
    * do not precisely match this object's format. With strict parsing, 
    * inputs must match this object's format. 
    * 
    * @param lenient when true, parsing is lenient 
    * @see java.util.Calendar#setLenient 
    */ 
    public void setLenient(boolean lenient) { 
     _format.setLenient(lenient); 
    } 

    /** 
    * Tell whether date/time parsing is to be lenient. 
    */ 
    public boolean isLenient() { 
     return _format.isLenient(); 
    } 
} 
+0

如果你按下箭头键,它工作吗?这是一个微调,而不是一个文本框。此外 - 我相信答案可以在DateEditor类中找到。请添加对您找到课程的页面的引用。 – 2010-08-19 08:19:46

+0

如果我使用箭头键,它可以工作。我在这里找到它: http://www.jarvana.com/jarvana/view/com/jidesoft/jide-oss/2.0.4/jide-oss-2.0.4-sources.jar!/com/jidesoft/ spinner/DateSpinner.java?format = ok – joLo 2010-08-19 08:21:56

当从event dispatch thread盯着,所述example正常工作使用任何箭头键,数字键或数字键盘的键。

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      JFrame f = new JFrame(); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      f.add(new DateSpinner()); 
      f.pack(); 
      f.setVisible(true); 
     } 
    }); 
} 
+0

问题仍然存在。 尝试突出显示时间微调器的HH部分并使用数字小键盘键输入数字,但没有任何反应,但如果尝试突出显示时间微调器的ss部分并使用数字小键盘键输入,现有数字将被覆盖 – joLo 2010-08-20 02:56:47

+0

@joLo:我可以重现你描述的模态行为。一旦选择了小时或分钟字段,左箭头或右箭头恢复键盘输入。如果需要,我应该可以更改键绑定:http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html – trashgod 2010-08-20 03:58:50