下面列出了怎么用javax.swing.SpinnerDateModel的API类实例代码及写法,或者点击链接到github查看源代码。
private JPanel createTimePanel() {
JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout());
Date date = getDate();
if (date == null) {
Calendar calendar = Calendar.getInstance(timeZone);
date = calendar.getTime();
}
SpinnerDateModel dateModel = new SpinnerDateModel(date, null, null, Calendar.DAY_OF_MONTH);
timeSpinner = new JSpinner(dateModel);
if (timeFormat == null) {
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
}
updateTextFieldFormat();
newPanel.add(new JLabel("Time:"));
newPanel.add(timeSpinner);
newPanel.setBackground(Color.WHITE);
return newPanel;
}
public JSpinnerDateEditor() {
super(new SpinnerDateModel());
dateFormatter = (SimpleDateFormat) DateFormat
.getDateInstance(DateFormat.MEDIUM);
((JSpinner.DateEditor) getEditor()).getTextField().addFocusListener(
this);
DateUtil dateUtil = new DateUtil();
setMinSelectableDate(dateUtil.getMinSelectableDate());
setMaxSelectableDate(dateUtil.getMaxSelectableDate());
((JSpinner.DateEditor)getEditor()).getTextField().setFocusLostBehavior(JFormattedTextField.PERSIST);
addChangeListener(this);
}
@Override
public Date getDate() {
if (date == null) {
return null;
}
return ((SpinnerDateModel) getModel()).getDate();
}
public void setDate(Date date, boolean updateModel) {
Date oldDate = this.date;
this.date = date;
if (date == null) {
((JSpinner.DateEditor) getEditor()).getFormat().applyPattern("");
((JSpinner.DateEditor) getEditor()).getTextField().setText("");
} else if (updateModel) {
if (dateFormatString != null) {
((JSpinner.DateEditor) getEditor()).getFormat().applyPattern(
dateFormatString);
}
((SpinnerDateModel) getModel()).setValue(date);
}
firePropertyChange("date", oldDate, date);
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
private JSpinner createDateSpinner(Calendar calendar) {
Date initDate = calendar.getTime();
calendar.add(Calendar.YEAR, -100);
Date earliestDate = calendar.getTime();
calendar.add(Calendar.YEAR, 200);
Date latestDate = calendar.getTime();
SpinnerDateModel spinnerDateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);
JSpinner dateSpinner = new JSpinner(spinnerDateModel);
dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/yyyy"));
dateSpinner.setName("date-spinner");
return dateSpinner;
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
static void createDateSpinner() {
Calendar calendar = Calendar.getInstance();
Date initDate = calendar.getTime();
calendar.add(Calendar.YEAR, -1);
Date earliestDate = calendar.getTime();
calendar.add(Calendar.YEAR, 1);
Date latestDate = calendar.getTime();
SpinnerModel dateModel = new SpinnerDateModel(initDate,
earliestDate,
latestDate,
Calendar.YEAR);
spinner = new JSpinner();
spinner.setModel(dateModel);
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
/**
* Returns a minimal value. Returns null if model is not one of the
* following: {@code javax.swing.SpinnerDateModel},
* {@code javax.swing.SpinnerListModel},
* {@code javax.swing.SpinnerNumberModel}. Also, returns null if the
* model does not have a minimal value.
*
* @return a minimal value.
*/
public Object getMinimum() {
SpinnerModel model = getModel();
if (model instanceof SpinnerNumberModel) {
return ((SpinnerNumberModel) model).getMinimum();
} else if (model instanceof SpinnerDateModel) {
return ((SpinnerDateModel) model).getEnd();
} else if (model instanceof SpinnerListModel) {
List<?> list = ((SpinnerListModel) model).getList();
return list.get(list.size() - 1);
} else {
return null;
}
}
/**
* Returns a maximal value. Returns null if model is not one of the
* following: {@code javax.swing.SpinnerDateModel},
* {@code javax.swing.SpinnerListModel},
* {@code javax.swing.SpinnerNumberModel}. Also, returns null if the
* model does not have a maximal value.
*
* @return a maximal value.
*/
public Object getMaximum() {
SpinnerModel model = getModel();
if (model instanceof SpinnerNumberModel) {
return ((SpinnerNumberModel) model).getMaximum();
} else if (model instanceof SpinnerDateModel) {
return ((SpinnerDateModel) model).getEnd();
} else if (model instanceof SpinnerListModel) {
List<?> list = ((SpinnerListModel) model).getList();
return list.get(list.size() - 1);
} else {
return null;
}
}
public ComparePanel()
{
dateSpinner = new JSpinner( new SpinnerDateModel() );
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSpinner, "dd.MM.yyyy");
dateSpinner.setEditor(dateEditor);
dateSpinner.setValue(new Date());
timeSpinner = new JSpinner( new SpinnerDateModel() );
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
timeSpinner.setEditor(timeEditor);
timeSpinner.setValue(new Date());
}
private void initDateModel() {
SpinnerDateModel dmodel = new SpinnerDateModel();
dateSpinner.setModel(dmodel);
dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MMM - dd - yyyy"));
SpinnerDateModel tmodel = new SpinnerDateModel();
timeSpinner.setModel(tmodel);
timeSpinner.setEditor(new JSpinner.DateEditor(timeSpinner, "HH:mm"));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
protected void addComponents(JPanel content, GridBagConstraints labelConstraints,
GridBagConstraints fieldConstraints) {
date = new JSpinner(new SpinnerDateModel(new Date(TimeHelper.today()), null, null, Calendar.DAY_OF_WEEK));
date.setEditor(new DateEditor(date, "MMM dd yyyy"));
JLabel dateLabel = new JLabel("Date:");
dateLabel.setFont(Styles.LABEL);
dateLabel.setHorizontalAlignment(SwingConstants.TRAILING);
content.add(dateLabel, labelConstraints);
content.add(date, fieldConstraints);
}
@Override
protected void buildContentsPanel() {
cmboPlanList = new JComboBox();
cmboPlanList.setEditable(true);
cmboPlanList.addActionListener(this);
spnYear = new JSpinner();
SpinnerDateModel spnMod = new SpinnerDateModel();
spnMod.setCalendarField( Calendar.YEAR );
spnYear.setModel( spnMod );
spnYear.setEditor( new JSpinner.DateEditor( spnYear, "yyyy" ) );
spnYear.setPreferredSize( new Dimension( 70,
spnYear.getPreferredSize().height ));
tfldDesc = new JTextField( 20 );
JPanel jplCont = new JPanel( new MigLayout( "gapy 0px!, insets 2px", "[align right][]") );
jplCont.add( new JLabel( "Plan:" ) );
jplCont.add( cmboPlanList, "wrap" );
jplCont.add( new JLabel( "Year:" ) );
jplCont.add( spnYear, "wrap" );
jplCont.add( new JLabel( "Description:" ) );
jplCont.add( tfldDesc, "wrap" );
jplCont.setBorder( BorderFactory.createEmptyBorder( 10, 10, 0, 10));
contentsPanelBuilt = true;
add( jplCont );
}
@Override
public Date getMaxSelectableDate() {
return (Date) ((SpinnerDateModel) getModel()).getEnd();
}
/**
*
*/
@Override
public Date getMinSelectableDate() {
return (Date) ((SpinnerDateModel) getModel()).getStart();
}
/**
*
*/
@Override
public void setMaxSelectableDate(Date max) {
((SpinnerDateModel) getModel()).setEnd(max);
}
/**
*
*/
@Override
public void setMinSelectableDate(Date min) {
((SpinnerDateModel) getModel()).setStart(min);
}
/**
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
@Override
public void stateChanged(ChangeEvent e) {
setDate(((SpinnerDateModel) getModel()).getDate(), false);
}