2017-07-01 116 views
0

我正在擴展JFormattedTextField以添加偵聽器。我有這個工作,雖然它可能不是最好的方式來做到這一點。有沒有辦法使用單個通用構造函數?延伸JFormattedTextField

public class TimeLineTextClass extends JFormattedTextField { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

private TimelineRecord timeLine; 

public TimeLineTextClass (NumberFormat tlformat_,HashMap<Integer,JComponent> fieldList_,int field_,TimelineRecord timeLine_) { 
    super(tlformat_); 
    timeLine=timeLine_; 
    getDocument().addDocumentListener(new DocumentListener() { 

     @Override 
     public void changedUpdate(DocumentEvent e) { 
      // Ignore - Using plain document 

     } 

     @Override 
     public void insertUpdate(DocumentEvent e) { 
      timeLine.setObject((String) getClientProperty("type"),getText()); 
     } 

     @Override 
     public void removeUpdate(DocumentEvent e) { 
      timeLine.setObject((String) getClientProperty("type"),getText()); 
     } 

    }); 
} 

public TimeLineTextClass (SimpleDateFormat tlformat_,HashMap<Integer,JComponent> fieldList_,int field_,TimelineRecord timeLine_) { 
    super(tlformat_); 
    timeLine=timeLine_; 
    getDocument().addDocumentListener(new DocumentListener() { 

     @Override 
     public void changedUpdate(DocumentEvent e) { 
      // Ignore - Using plain document 

     } 

     @Override 
     public void insertUpdate(DocumentEvent e) { 
      timeLine.setObject((String) getClientProperty("type"),(String) getText()); 
     } 

     @Override 
     public void removeUpdate(DocumentEvent e) { 
      timeLine.setObject((String) getClientProperty("type"),(String) getText()); 
     } 

    }); 
} 

} 

似乎應該有一種方法,只使用一個具有泛型格式類型的構造函數作爲第一個參數和'超級'。 TIA。

+0

爲什麼要從JFormattedTextField類擴展?您可以在創建JFormatterTextField對象後添加偵聽器。 – Progman

+0

是的,我可以但有2個原因。首先是因爲有人建議我在這裏根據我想要如何使用它的另一個問題這樣做。第二個原因是這是一個學習擴展類的機會。 –

回答

1

就像JFormattedTextField構造函數,你可以使用類型Format趕上NumberFormat類型和SimpleDateFormat類型。

+0

正是我在找的東西。謝謝。 –