2013-10-17 61 views
0

我正在創建一個垂直增長的文本區域,我仍然可以看到文本的第一行。可擴展的文本區域在wicket

目前我正在嘗試使用此:

public class GrowableTextArea extends TextArea { 

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

    int initialRowSize = 1; 

    int fixedColSize = 40; 

    int currentRowSize = initialRowSize; 

    float temp = 1.0f; 

    String inputValue; 

    public GrowableTextArea(String id) { 

     super(id); 

     setModel(new PropertyModel(this, inputValue)); 

     setOutputMarkupId(true); 

     add(new GrowableBehavior("onkeyup")); 

    } 

    /*** 
    * Set the rows here. So that every time component is rendered, 
    * 
    * rows value is updated. 
    **/ 

    @Override 
    public void onComponentTag(ComponentTag tag) { 

     super.onComponentTag(tag); 

     tag.put("rows", currentRowSize); 

     tag.put("cols", fixedColSize); 

    } 

    /*** Getters and setters ***/ 

    public int getCurrentRowSize() { 

     return currentRowSize; 

    } 

    public void setCurrentRowSize(int currentRowSize) { 

     this.currentRowSize = currentRowSize; 

    } 

    public String getInputValue() { 

     return inputValue; 

    } 

    public void setInputValue(String inputValue) { 

     this.inputValue = inputValue; 

    } 


    private class GrowableBehavior extends AjaxFormComponentUpdatingBehavior 

    { 

     /*** 
     * Tell it to fire the onkeyup event every 1/2 sec. 
     **/ 

     public GrowableBehavior(String event) { 

      super(event); 

      setThrottleDelay(Duration.milliseconds(500)); 

     } 

     /*** 
     * First the model is updated and then 
     * 
     * length is checked to see the 
     * 
     * whether rowcount should be incremented 
     **/ 

     @Override 
     protected void onUpdate(AjaxRequestTarget target) { 

      Component comp = getComponent(); 

      TextArea textarea = (TextArea) comp; 

      String value = (String) textarea.getConvertedInput(); 

      if (value != null) { 

       int currentLength = value.length(); 

       float f = (float) currentLength/fixedColSize; 

       if (f > temp) { 
        currentRowSize++; 

        temp = temp + 1; 

        target.add(comp); 
       } 

      } 

     } 
    } 

    public void setThrottleDelay(Duration milliseconds) { 
     // TODO Auto-generated method stub 

    } 

} 

我的輸出是隻是一個普通的文本區域,類似的堆棧溢出文本區域。這裏有什麼問題?

+0

恕我直言,這是更好地與普通的JavaScript完成(無需額外的服務器調用只是爲了調整文本區)。您是否試圖在「onUpdate」某處設置斷點來檢查帶有「onkeyup」事件的GrowableBehavior是否正在運行?編輯:也許「getConvertedInput」爲空;嘗試使用「getRawInput」... – mrak

+0

感謝您的提示,但我將使用下面的自動尺寸示例 –

回答