Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a tooltip above a cell in a JTable

I need to show a tooltip above (or below :) a cell when the user enter a wrong value in it (see the image below). I have a tooltip, but I need a Point to display it at the right position, so I want to get a cell position. Do you know how to get this?

BUT, if you have a better solution to realize this behaviour, I'm open to all proposition (especially for the fact that the tooltip is not bind with the cell/Jtable/Panel and if I move/close/minmize my window the tooltip is display at the same position)

alt text

Thanks, Damien

like image 809
Damien Avatar asked Feb 03 '10 17:02

Damien


People also ask

How can you change the appearance of data in cells in JTable?

We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.

Which method is used to add tooltip text?

We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.

How can we add insert a JButton to JTable cell in Java?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.

How do I get the value of a JTable cell?

Obtaining Cell Values To get the value from a particular grid cell, you can use the wValue property of the JTable object. The property has the Row and Column parameters which specify the row and the column that contain the cell.


2 Answers

Please refer below code snippet, and you'll get the 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 only show the specific cell, all you have to do is change the column param in the params of getValueAt(...) method to a specific column which contains that cell

like image 124
Dzung Nguyen Avatar answered Oct 05 '22 20:10

Dzung Nguyen


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

Edit: In fact, it is not really a tooltip that you need here, as the tooltip need to have the cursor positionned over the cell. You want to display the tooltip even if the cursor is outside the cell, right?

Anyway, an alternative solution is to change the background of the cell when the value entered by the user is invalid (in orange or red for example), and then add a "real" tooltip (using the link I provided) in order to give the user a complete error message.

like image 42
Romain Linsolas Avatar answered Oct 05 '22 18:10

Romain Linsolas