如何使溢出的TextArea截断文本并最终显示省略号?

时间:2011-07-19 12:31:44

标签: java swing jtextarea

我知道这似乎是一个愚蠢的问题,但到目前为止我找到的所有答案都要求我使用html标签。有更简单的方法吗? TextArea可能会改变大小。

2 个答案:

答案 0 :(得分:1)

覆盖Document的{​​{3}}方法,这样无论何时插入字符串,都会删除多余的字符并插入省略号。 这是一个例子:

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);
        }
    } 
});

答案 1 :(得分: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);
    }
}