javamonkey79
javamonkey79

Reputation: 17775

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!       |
=============================

I've considered a couple solutions:

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!

Upvotes: 2

Views: 5606

Answers (3)

mzagorski
mzagorski

Reputation: 1

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.sstatic.net/3LHyc.png

Upvotes: 0

Ioannis Soultatos
Ioannis Soultatos

Reputation: 120

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

Upvotes: 0

camickr
camickr

Reputation: 324108

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.

Upvotes: 5

Related Questions