开发者

JTable Cells - Handling Long Text

I have a JTable where one column occasionally has a fair amount of text in it. There are algorithms that we're using to expand the height of each row to the tallest cell. The problem is for long text cells we get "fat" rows.

It looks something like this:

=============================
| Col1 | Col2 | This is some|
|      |      | very long   |
|      |      | text!       |
===开发者_如何学JAVA==========================

I've considered a couple solutions:

  • Clipping the text and adding a mouse listener to "expand" the clipped text
  • Clipping the text and adding a tooltip or dialog to show the extra contents

Does anyone know of any libraries that fix this? I'm open to using some other technique...I'm not convinced my solution is the best.

Thanks in advance!


I would just use a tooltip.

You can override the getToolTipText(() method of JTable to do this.

JTable table = new JTable(...)
{
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
};

Or if its only for certain columns you can use the renderer to set the tooltip text. See Specifying Tool Tips for Cells.


It took me a while to find out how to display the tooltip on Netbeans but your answer helped so much. here it is implemented on netbeans GUI Builder... right click on your Jtable->customize code. choose "custom creation" where the new "javax.swing.JTable();" code is and before the semi colon ; add the code of the answer below... looks like this:

YourTable = new javax.swing.JTable(){
    //add tooltip to display the full cell text when not displayed
    public String getToolTipText( MouseEvent e )
    {
        int row = rowAtPoint( e.getPoint() );
        int column = columnAtPoint( e.getPoint() );

        Object value = getValueAt(row, column);
        return value == null ? null : value.toString();
    }
}
;


Adding a mouse motion listener to your JTable, getting the value of where the mouse is at, then adding the long value to a tooltip is an efficient way to easily see the longer table cell value when the mouse is hovered over that row:

    JTable yourTable = new JTable(<the data>, <the fields>);

    // Add a mouse motion listener to your JTable
    yourTable.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            // do nothing
        }

        @Override
        public void mouseMoved(MouseEvent e) {

            // Set the tooltip to an empty string
            yourTable.setToolTipText("");

            // Get the table cell value where the mouse is located
            String value = (String) yourTable.getValueAt(yourTable.rowAtPoint(e.getPoint()),
                    yourTable.columnAtPoint(e.getPoint()));

            // If the length of the value is greater than some number... 
            if (value.length() > 50) {

                // add a tooltip to the JTable that shows the value
                yourTable.setToolTipText(value);
            }
        }
    });

See result here: https://i.stack.imgur.com/3LHyc.png

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜