2013-04-22 42 views
-1

Text的內容超過Text的寬度時,我想在文本字段的末尾顯示點(...)。如何在文本超出文本大小時顯示點?

我試圖使用ModifyListener沒有成功。任何有關這方面的幫助將不勝感激。

@Override 
     public void modifyText(ModifyEvent e) { 
      // TODO Auto-generated method stub 



      if (wakeupPatternText.getText().length()>=12) { 


       String wakeuppattern=wakeupPatternText.getText(0, 11); 




       String dot="..."; 

       String wakeup=wakeuppattern+dot; 
wakeupPatternText.setText(wakeup); 




     }  


     } 
    }); 
+1

如果您嘗試過某些方法,請始終解釋您嘗試的內容。 – Baz 2013-04-22 06:42:34

+0

你可以打印出任何日誌並進行調試嗎?如果您需要幫助,請向我們展示您編寫的代碼片段;導入部分應該是偵聽器,它應該在其中包含文本的子字符串。 – 2013-04-22 07:13:41

+0

@Baz我試圖添加一個修改偵聽器,但主要問題是假設我有30個字符的文本,我的文本框只支持15個字符我正在追加..但作爲我的按鈕綁定文本框所有獲取禁用爲(..)是視爲無效值。 – Rajesh 2013-04-22 07:16:46

回答

1

這應該做你想要什麼:

private static String textContent = ""; 

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout(SWT.VERTICAL)); 

    Text text = new Text(shell, SWT.BORDER); 

    text.addListener(SWT.FocusOut, new Listener() 
    { 
     @Override 
     public void handleEvent(Event e) 
     { 
      Text text = (Text) e.widget; 
      textContent = text.getText(); 

      text.setText(textContent.substring(0, Math.min(10, textContent.length())) + "..."); 
     } 
    }); 

    text.addListener(SWT.FocusIn, new Listener() 
    { 
     @Override 
     public void handleEvent(Event e) 
     { 
      Text text = (Text) e.widget; 

      text.setText(textContent); 
     } 
    }); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Lose focus"); 

    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

它偵聽焦點事件並截斷可見String如果太長。

這是它的外觀:

隨着重點:

enter image description here

沒有焦點:

enter image description here


之所以你的代碼不工作是以下幾點:當你c來自VerifyListener的所有wakeupPatternText.setText(wakeup);偵聽器本身再次被遞歸地調用。

+0

Ooops ....對不起,這愚蠢的misatke..anywayz再次感謝baz – Rajesh 2013-04-22 09:09:20

+0

@Rajesh不客氣。正如我所說的,如果人們看到你已經嘗試過的東西以及你被困在哪裏,他們就會更有動力去幫助。 – Baz 2013-04-22 10:27:20

相關問題