Like i discribed in the title, my Spinner shows the week of year from my DB and a formated text field shows the first day of this given week. Everything works fine to this point, but if i try to change the Week of the Spinner the Date jumps to 1970.
This is my SpinnerModel:
spinner_KW.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.WEEK_OF_YEAR));
And here is the Code where I set the value from the DB and the DateEditor:
public void setDateKWSpinner(Date kw){
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(spinner_KW, "w");
spinner_KW.setEditor(timeEditor);
spinner_KW.setValue(kw); // will only show the current time
setWeekfromto();
}
I realy hope my Problem isn't as dumb as I think it is -.-
UPDATE
As requestet here is a smal running code that shows the Problem:
package datespinner_test;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JSpinner;
public class SpinnerJFrame extends javax.swing.JFrame {
public SpinnerJFrame() {
initComponents();
setDateKWSpinner();
setWeekfrom();
}
public void setDateKWSpinner(){
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(dateSpinner, "w");
dateSpinner.setEditor(timeEditor);
dateSpinner.setValue(new Date());
}
public void setWeekfrom(){
Calendar cal = Calendar.getInstance();
cal.setTime((Date) dateSpinner.getModel().getValue());
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
firstDayofWeek.setValue(cal.getTime());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
dateSpinner = new javax.swing.JSpinner();
firstDayofWeek = new javax.swing.JFormattedTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
dateSpinner.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.WEEK_OF_YEAR));
dateSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
dateSpinnerStateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(dateSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(firstDayofWeek, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(190, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dateSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(firstDayofWeek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void dateSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {
setWeekfrom();
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SpinnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SpinnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SpinnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SpinnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpinnerJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JSpinner dateSpinner;
private javax.swing.JFormattedTextField firstDayofWeek;
// End of variables declaration
}