开发者

How to get JTextPane to display ellipsis to signal text overflow?

开发者 https://www.devze.com 2023-03-18 11:46 出处:网络
I am using JTextPane as a table cell renderer to di开发者_开发知识库splay rich text. When the text is too long to fit inside a cell, it is truncated. I would like to mimic the JLabel behavior, i.e. sh

I am using JTextPane as a table cell renderer to di开发者_开发知识库splay rich text. When the text is too long to fit inside a cell, it is truncated. I would like to mimic the JLabel behavior, i.e. show ellipsis (...) to alert the user that part of the text is not visible. Has anyone done this before?


Solution I ended up adopting, with help from StanislavL. The algorithm works by chopping off one character at a time off the end of StyledDocument, appending "..." and comparing resulting preferred width to table cell width. This is inefficient, especially in case of very long strings, but not a problem in my case. Can be optimized. The following goes into your renderer's getTableCellRendererComponent

    m_dummyTextPane.setDocument(doc);
    m_dummyTextPane.setSize(Short.MAX_VALUE, table.getRowHeight());        
    int width = m_dummyTextPane.getPreferredSize().width;
    int start = doc.getLength() - 1;
    while(width >= table.getColumnModel().getColumn(col).getWidth() && start>0) {  
        try {
            doc.remove(Math.min(start, doc.getLength()), 
                    doc.getLength() - Math.min(start, doc.getLength()));
            doc.insertString(start, "...", null);
        } catch (BadLocationException e) {
            e.printStackTrace();
            break;
        }
        start--;
        width = m_dummyTextPane.getPreferredSize().width;
    }


You can use this http://java-sl.com/tip_text_height_measuring.html to measure content for the fixed width. If it requires more space than available just paint something over the JTextPane.

I like the trashgod's idea with scroll too. (+1)


If a scroll bar is an acceptable alternative, but space is at a premium, you may be able to specify a JComponent.sizeVariant, as discussed in Resizing a Component and Using Client Properties.


I did it by just overriding the paint() and getToolTipText() methods, to put it in the tooltip if it's too long:

public void paint(Graphics g)
{
     frc=((Graphics2D)g).getFontRenderContext();
     super.paint(g);
}

public String getToolTipText(MouseEvent e)
{
    String tip=null;
    java.awt.Point p=e.getPoint();
    int colnum=columnModel.getColumnIndexAtX(p.x);
    int rowIndex=rowAtPoint(p);
    String field=(String)getModel().getValueAt(rowIndex, colnum);

    if (getColumnModel().getColumn(colnum).getWidth()< getFont().getStringBounds(field,frc).getWidth())
    {
        int i=0;
        StringBuffer buf=new StringBuffer("<html>");
        while (i<field.length())
        {
            buf.append(field.substring(i, Math.min(field.length(),i+100)));
            buf.append("<br>");
            i+=100;
        }
        tip=buf.toString();
    }
    return tip;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消