📄 jspinner.java
字号:
* * @see SimpleDateFormat#SimpleDateFormat(String) */ public DateEditor(JSpinner spinner, String dateFormatPattern) { super(spinner); init(new SimpleDateFormat(dateFormatPattern)); } /** * Initializes the JFormattedTextField for this editor. * * @param format the date format to use in the formatted text field */ private void init(SimpleDateFormat format) { dateFormat = format; getTextField().setFormatterFactory( new JFormattedTextField.AbstractFormatterFactory() { public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField ftf) { return new DateFormatter(dateFormat); } }); } /** * Returns the <code>SimpleDateFormat</code> instance that is used to * format the date value. * * @return the <code>SimpleDateFormat</code> instance that is used to * format the date value */ public SimpleDateFormat getFormat() { return dateFormat; } /** * Returns the {@link SpinnerDateModel} that is edited by this editor. * * @return the <code>SpinnerDateModel</code> that is edited by this editor */ public SpinnerDateModel getModel() { return (SpinnerDateModel) getSpinner().getModel(); } } private static final long serialVersionUID = 3412663575706551720L; /** DOCUMENT ME! */ private SpinnerModel model; /** DOCUMENT ME! */ private JComponent editor; /** DOCUMENT ME! */ private ChangeListener listener = new ChangeListener() { public void stateChanged(ChangeEvent evt) { fireStateChanged(); } }; /** * Creates a JSpinner with <code>SpinnerNumberModel</code> * * @see javax.swing.SpinnerNumberModel */ public JSpinner() { this(new SpinnerNumberModel()); } /** * Creates a JSpinner with the specific model and sets the default editor * * @param model DOCUMENT ME! */ public JSpinner(SpinnerModel model) { this.model = model; model.addChangeListener(listener); setEditor(createEditor(model)); updateUI(); } /** * If the editor is <code>JSpinner.DefaultEditor</code>, then forwards the * call to it, otherwise do nothing. * * @throws ParseException DOCUMENT ME! */ public void commitEdit() throws ParseException { if (editor instanceof DefaultEditor) ((DefaultEditor) editor).commitEdit(); } /** * Gets the current editor * * @return the current editor * * @see #setEditor */ public JComponent getEditor() { return editor; } /** * Changes the current editor to the new editor. This methods should remove * the old listeners (if any) and adds the new listeners (if any). * * @param editor the new editor * * @throws IllegalArgumentException DOCUMENT ME! * * @see #getEditor */ public void setEditor(JComponent editor) { if (editor == null) throw new IllegalArgumentException("editor may not be null"); if (this.editor instanceof DefaultEditor) ((DefaultEditor) editor).dismiss(this); else if (this.editor instanceof ChangeListener) removeChangeListener((ChangeListener) this.editor); if (editor instanceof ChangeListener) addChangeListener((ChangeListener) editor); this.editor = editor; } /** * Gets the underly model. * * @return the underly model */ public SpinnerModel getModel() { return model; } /** * Sets a new underlying model. * * @param newModel the new model to set * * @exception IllegalArgumentException if newModel is <code>null</code> */ public void setModel(SpinnerModel newModel) { if (newModel == null) throw new IllegalArgumentException(); if (model == newModel) return; SpinnerModel oldModel = model; model = newModel; firePropertyChange("model", oldModel, newModel); if (editor == null) setEditor(createEditor(model)); } /** * Gets the next value without changing the current value. * * @return the next value * * @see javax.swing.SpinnerModel#getNextValue */ public Object getNextValue() { return model.getNextValue(); } /** * Gets the previous value without changing the current value. * * @return the previous value * * @see javax.swing.SpinnerModel#getPreviousValue */ public Object getPreviousValue() { return model.getPreviousValue(); } /** * Gets the <code>SpinnerUI</code> that handles this spinner * * @return the <code>SpinnerUI</code> */ public SpinnerUI getUI() { return (SpinnerUI) ui; } /** * Gets the current value of the spinner, according to the underly model, * not the UI. * * @return the current value * * @see javax.swing.SpinnerModel#getValue */ public Object getValue() { return model.getValue(); } /** * DOCUMENT ME! * * @param value DOCUMENT ME! */ public void setValue(Object value) { model.setValue(value); } /** * This method returns a name to identify which look and feel class will be * the UI delegate for this spinner. * * @return The UIClass identifier. "SpinnerUI" */ public String getUIClassID() { return "SpinnerUI"; } /** * This method resets the spinner's UI delegate to the default UI for the * current look and feel. */ public void updateUI() { setUI((SpinnerUI) UIManager.getUI(this)); } /** * This method sets the spinner's UI delegate. * * @param ui The spinner's UI delegate. */ public void setUI(SpinnerUI ui) { super.setUI(ui); } /** * Adds a <code>ChangeListener</code> * * @param listener the listener to add */ public void addChangeListener(ChangeListener listener) { listenerList.add(ChangeListener.class, listener); } /** * Remove a particular listener * * @param listener the listener to remove */ public void removeChangeListener(ChangeListener listener) { listenerList.remove(ChangeListener.class, listener); } /** * Gets all the <code>ChangeListener</code>s * * @return all the <code>ChangeListener</code>s */ public ChangeListener[] getChangeListeners() { return (ChangeListener[]) listenerList.getListeners(ChangeListener.class); } /** * Fires a <code>ChangeEvent</code> to all the <code>ChangeListener</code>s * added to this <code>JSpinner</code> */ protected void fireStateChanged() { ChangeEvent evt = new ChangeEvent(this); ChangeListener[] listeners = getChangeListeners(); for (int i = 0; i < listeners.length; ++i) listeners[i].stateChanged(evt); } /** * Creates an editor for this <code>JSpinner</code>. Really, it should be a * <code>JSpinner.DefaultEditor</code>, but since that should be * implemented by a JFormattedTextField, and one is not written, I am just * using a dummy one backed by a JLabel. * * @param model DOCUMENT ME! * * @return the default editor */ protected JComponent createEditor(SpinnerModel model) { if (model instanceof SpinnerDateModel) return new DateEditor(this); else if (model instanceof SpinnerNumberModel) return new NumberEditor(this); else return new DefaultEditor(this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -