2015-04-08 73 views
1

我正在嘗試創建具有小時和分鐘的微調。分鐘部分只需要增加10分鐘,時間必須從當前時間到結束時間。我還需要最小值(之前的當前時間)來更新到當前時間。JSpinner時間限制

我試着玩過它,但我無法讓它工作。

JSpinner spinner1 = new javax.swing.JSpinner(); 
    SpinnerDateModel spinnermodel = new SpinnerDateModel(); 
    spinnermodel.setCalendarField(Calendar.MINUTE); 
    spinner1.setModel(spinnermodel); 
    spinner1.setEditor(new JSpinner.DateEditor(spinner1, "hh:mm")); 

SpinnerModel model = new SpinnerDateModel(currentDate, currentDate, latestDate, Calendar.MINUTE * 10 ?); 

回答

3

SpinnerDateModel只是使用1來遞增您想要更改的字段。

我延長SpinnerDateModel發生加成屬性添加到模型來控制增量值而不是硬編碼到1:

import java.util.*; 
import javax.swing.*; 

public class MySpinnerDateModel extends SpinnerDateModel 
{ 
    private int increment = 1; 

    public MySpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) 
    { 
     super(value, start, end, calendarField); 
    } 

    public MySpinnerDateModel() 
    { 
     this(new Date(), null, null, Calendar.DAY_OF_MONTH); 
    } 

    public void setIncrement(int increment) 
    { 
     this.increment = increment; 
    } 

    public int getIncrement() 
    { 
     return increment; 
    } 

    @Override 
    public Object getNextValue() 
    { 
     Calendar cal = Calendar.getInstance(); 
     Date value = (Date)getValue(); 
     cal.setTime(value); 
     cal.add(getCalendarField(), increment); 
     Date next = cal.getTime(); 
     Comparable end = getEnd(); 

     return ((end == null) || (end.compareTo(next) >= 0)) ? next : null; 
    } 

    @Override 
    public Object getPreviousValue() 
    { 
     Calendar cal = Calendar.getInstance(); 
     Date value = (Date)getValue(); 
     cal.setTime(value); 
     cal.add(getCalendarField(), -increment); 
     Date prev = cal.getTime(); 
     Comparable start = getStart(); 

     return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null; 
    } 
} 

你應該能夠使用你的模型前,但做的方式一條附加聲明:

MySpinnerDateModel model = new MySpinnerDateModel(currentDate, currentDate, latestDate, Calendar.MINUTE); 
model.setIncrement(10); 
0

您可以擴展SpinnerDateModel以指定行爲。下面是getNextValue和getPreviousValue被覆蓋以返回值+/- 10分鐘的示例:

Date now = new Date(); 
    Date start = now; 
    final long tenMinutesInMillis = 1000 * 60 * 10; 
    Date end = new Date(now.getTime() + tenMinutesInMillis * 60); 
    SpinnerModel model = new SpinnerDateModel(now, start, end, Calendar.MINUTE){ 
     @Override 
     public Object getNextValue(){ 
      Date newDate = new Date(getDate().getTime() + tenMinutesInMillis); 
      Date endDate = (Date)getEnd(); 
      return newDate.getTime() > endDate.getTime() ? endDate : newDate; 
     } 

     @Override 
     public Object getPreviousValue(){ 
      Date newDate = new Date(getDate().getTime() - tenMinutesInMillis); 
      Date startDate = (Date)getStart(); 
      return newDate.getTime() < startDate.getTime() ? startDate : newDate; 
     } 
    }; 
+1

這不會執行任何範圍檢查。下一個/前一個值應該落在開始/結束範圍內。另外,如果你想增加幾天呢? – camickr

+0

謝謝 - 編輯範圍檢查記住。剛剛看到你的答案,並喜歡它在這個快速和骯髒的選擇(更靈活的增量時間)。 – copeg

+1

我剛剛從'SpinnerDateModel'類中複製了現有代碼,並將其修改爲使用變量作爲增量/減量值。我儘量避免在可能的情況下重寫代碼。 – camickr