2015-06-13 24 views
0

我有一個JSpinner我添加到JPanel我想它的時間設置爲:GregorianCalendar calendar如何設置JSpinner的價值某一日期

JSpinner spinner = new JSpinner(); 
spinner.setModel(model); 
pom.add(new JLabel("Date", JLabel.RIGHT)); 
pom.add(spinner); 

如何做到這一點,因爲我得到IllegalValue例外。

+0

你可能會使用'spinner.setValue()'設置一些價值。如果沒有,那就使用它。如果您正在使用該代碼,請在此處發佈該代碼,並提及您所得到的確切錯誤。 –

+0

也看看這個問題:http://stackoverflow.com/questions/25105375/how-to-set-value-of-jspinner-from-string。它可能有幫助。 –

+0

首先閱讀[如何使用Spinners] Swing教程中的部分(http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html)以獲取工作示例。如果你想顯示日期,那麼你將需要使用'SpinnerDateModel'。 – camickr

回答

0

一個人應該:

 SpinnerDateModel model = new SpinnerDateModel(); 
     model.setCalendarField(Calendar.DAY_OF_YEAR); 

     JSpinner spinner = new JSpinner(); 
     spinner.setModel(model); 

     SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
     java.sql.Time time = new java.sql.Time(osoba.getDataZatrudnienia().getTime().getTime()); 
     spinner.setValue(time);