如何使溢出的TextArea截断文本并最后显示省略号? [英] How to make an overflowed TextArea truncate text and show ellipsis in the end?

查看:398
本文介绍了如何使溢出的TextArea截断文本并最后显示省略号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this seems to be a stupid question, but so far all the answers I found ask me to use html tags. Is there a easier way to do it? The TextArea may change size.

推荐答案

执行此操作的正确方法是创建一个自定义视图,在需要时绘制一个省略号.但由于我不知道如何执行此操作,因此我将尝试使用一些技巧,您也许可以使用:

The proper way to do this is to create a custom View that paints an ellipsis when required. But since I have no idea how to do this I'll try a little hack you might be able to use:

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

这篇关于如何使溢出的TextArea截断文本并最后显示省略号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆