2011-07-19 116 views
1

我知道這似乎是一個愚蠢的問題,但到目前爲止我找到的所有答案都要求我使用html標籤。有沒有更簡單的方法來做到這一點? TextArea可能會改變大小。如何使溢出的TextArea截斷文本並在最後顯示省略號?

+0

溢出了怎麼樣?你沒有把它放在JScrollPane中嗎? – jzd

+0

不幸的是我的應用程序不允許滾動窗格。我想我只會擴展JTextArea來添加截斷功能。 – stackoverflower

+0

TextArea僅顯示內容的片段。如果用戶單擊按鈕,則會顯示完整內容。這有點像谷歌的搜索頁面,只顯示每個網頁的片段。 – stackoverflower

回答

1

覆蓋DocumentinsertString方法,以便每當插入一個字符串時,多餘的字符將被刪除並插入一個省略號。 下面是一個例子:

JTextArea area = new JTextArea(); 
area.setDocument(new PlainDocument() { 
    private static final long serialVersionUID = 1L; 
    private static final int MAX = 100; 

    @Override 
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 
     super.insertString(offs, str, a); 

     //has the length been exceeded 
     if(getLength() > MAX) { 

      //remove the extra characters. 
      //need to take into account the ellipsis, which is three characters. 
      super.remove(MAX - 3, getLength() - MAX + 3); 

      //insert ellipsis 
      super.insertString(getLength(), "...", a); 
     } 
    } 
}); 
+0

也考慮[...(U + 2026)](http://www.fileformat.info/info/unicode/char/2026/index.htm)。 – trashgod

1

正確的方式做,這是創建一個自定義視圖需要時繪製一個省略號。但由於我不知道如何做到這一點,我會嘗試一下你可能會用到的一些小技巧:

// This comment is here so that the text will wrap to the next line and you should see the ellipsis, 
// indicating that there is more text. 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.swing.*; 
import javax.swing.text.*; 

class TextAreaEllipsis 
{ 
    public static void main(String a[]) 
    { 
     JTextArea textArea = new JTextArea(4, 15) 
     { 
      protected void paintComponent(Graphics g) 
      { 
       super.paintComponent(g); 

       int preferredHeight = (int)getUI().getRootView(this).getPreferredSpan(View.Y_AXIS); 

       if (preferredHeight > getSize().height) 
        paintEllipsis(g); 
      } 

      private void paintEllipsis(Graphics g) 
      { 
       try 
       { 
        int caretWidth = 1; 
        FontMetrics fm = getFontMetrics(getFont()); 
        String ellipsis = "..."; 
        int ellipsisWidth = fm.stringWidth(ellipsis) + caretWidth; 

        Insets insets = getInsets(); 
        int lineWidth = getSize().width - insets.right; 
        Point p = new Point(lineWidth, getSize().height - 1); 

        int end = viewToModel(p); 
        Rectangle endRectangle = modelToView(end); 
        int start = end; 
        Rectangle startRectangle = endRectangle; 
        int maxWidth = lineWidth - ellipsisWidth; 

        while (startRectangle.x + startRectangle.width > maxWidth) 
        { 
         startRectangle = modelToView(--start); 

        } 

        Rectangle union = startRectangle.union(endRectangle); 
        g.setColor(getBackground()); 
        g.fillRect(union.x + caretWidth, union.y, union.width, union.height); 
        g.setColor(getForeground()); 
        g.drawString("...", union.x + caretWidth, union.y + union.height - fm.getDescent()); 
       } 
       catch(BadLocationException ble) 
       { 
        System.out.println(ble); 
       } 
      } 
     }; 

     textArea.setLineWrap(true); 
     textArea.setWrapStyleWord(true); 
     textArea.setPreferredSize(textArea.getPreferredScrollableViewportSize()); 

     try 
     { 
      FileReader reader = new FileReader("TextAreaEllipsis.java"); 
      BufferedReader br = new BufferedReader(reader); 
      textArea.read(br, null); 
      br.close(); 
     } 
      catch(Exception e2) { System.out.println(e2); } 

     JFrame frame = new JFrame("TextArea Ellipsis"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(textArea); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
}