Java Swing: Showing tool tip in JTable based on text under mouse pointer
Asked Answered
H

1

1

I have a JTable where I display some string data formatted using html. I need to show a tool tip based on the text under the mouse pointer

enter image description here

On mouse over "Line1" and "Line2" I need to show different tool tips. Is there any way to achieve this or do I have to use a custom renderer to render each line with a cell and show tool tip based on that ?

Here's the sample code to create the table

package com.sample.table;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

public class SampleTable {

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SampleTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(createTablePanel(), BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static JPanel createTablePanel(){
    JPanel tablePanel = new JPanel();

    JTable table = createTable();
    table.setFillsViewportHeight(true);
    table.setRowHeight(45);
    addListener(table);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setPreferredSize(new Dimension(300, 120));

    tablePanel.add(scrollPane);

    return tablePanel;
}

private static void addListener(JTable table) {
    table.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent e) {
            if(e.getSource() instanceof JTable){
                JTable table = (JTable)e.getSource();

                table.setToolTipText("Some tooltip");
            }

        }

        @Override
        public void mouseDragged(MouseEvent e) {
            // do nothing

        }
    });

}

public static JTable createTable(){
    String[] columnNames = {"Column1", "Column2"};
    Object[][] data = {{"1", "<html>Line1<br/>Line2</html>"},
                        {"2", "<html>Line1<br/>Line2</html>"}};

    JTable table = new JTable(data, columnNames);

    return table;
}



public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}
Homeopathy answered 27/4, 2012 at 14:47 Comment(0)
K
3

Override the getToolTipText(MouseEvent) method of the component which your TableCellRenderer returns. If you are extending DefaultTableCellRenderer which extends JLabel and returns itself as renderer component, you can override it directly in your subclass.

You should be able to determine over which line the mouse pointer is using MouseEvent#getPoint(). See JTextComponent#viewToModel or JTextArea#getLineOfOffset (if you are using a JTextArea for rendering instead of a JLabel).

Korykorzybski answered 27/4, 2012 at 15:0 Comment(4)
But I would still be able to get only the entire value in the cell (which is '<html>Line1<br/>Line2</html>') and not the actual line ?Homeopathy
No, you should be able to determine over which line it is using the point in the mouse event. See JTextComponent#viewToModel and JTextArea#getLineOfOffset (oh, that would require you use a JTextArea for rendering instead of a JLabel)Korykorzybski
I haven't had the time to try this out completely but using a JEditorPane might be more better than JTextArea as I am using html. I am accepting this answer as it is close enough :)Homeopathy
TableCellRenderer doesn't have getToolTipText how are you meant to override it?Madwort

© 2022 - 2024 — McMap. All rights reserved.