Show tooltip above cell in JTable

I need to show a tooltip above (or below :) the cell when the user enters an incorrect value into it (see. Image below). I have a tooltip, but I need a dot to display it in the correct position, so I want to get the position of the cell. Do you know how to get this?

BUT, if you have a better solution for implementing this behavior, I am open to all suggestions (especially for the fact that the tooltip is not connected to the / Jtable / Panel cell and if I move / close / minimize the window, the tooltip is the display in that same position)

alt text

Thanks Damien

+6
java swing tooltip jtable
source share
5 answers
+1
source share

Please refer to the code snippet and you will get a solution

JTable table = new JTable() { public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); if (c instanceof JComponent) { JComponent jc = (JComponent) c; jc.setToolTipText(getValueAt(row, column).toString()); } return c; } }; 

If you want to show only a specific cell, all you have to do is change the column parameter in the getValueAt (...) method parameters for the specific column that contains this cell

+6
source share

You have an example of such a feature in the visual guide to Swing components.

Edit: This is actually not the tooltip you need here, since the tooltip should have a cursor located above the cell. You want to display a tooltip even if the cursor is outside the cell, right?

In any case, an alternative solution is to change the cell background when the value entered by the user is invalid (for example, orange or red), and then add a β€œreal” tooltip (using the link I provided) to give the user a complete error message.

+2
source share

Just use the code below when creating the JTable.

 JTable auditTable = new JTable(){ //Implement table cell tool tips. public String getToolTipText(MouseEvent e) { String tip = null; java.awt.Point p = e.getPoint(); int rowIndex = rowAtPoint(p); int colIndex = columnAtPoint(p); try { //comment row, exclude heading if(rowIndex != 0){ tip = getValueAt(rowIndex, colIndex).toString(); } } catch (RuntimeException e1) { //catch null pointer exception if mouse is over an empty line } return tip; } }; 
+2
source share

Use the following code to get the value for the correct string if using RowSorter:

 jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString()); 
0
source share

All Articles