0

I'm using a JTable (specifically a JXTable) and when I hover over a cell I would like to present some sort of JPanel or Component (to show some graphical data using JFreeChart)

I know the JTable has this:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getToolTipText(java.awt.event.MouseEvent)

but that is obviously just a way to show a textual tooltip. Any ideas or solutions to do this?

mainstringargs
  • 13,563
  • 35
  • 109
  • 174

2 Answers2

2

You can show a pop up on mouse over and you can customize the pop up window however you want. I hope the below piece of code would help.

JFrame frame = new JFrame("Sort Records based on Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Object rows[][] = { { "Loganathan Mohanraj", 31 },
    { "Renuka Loganathan", 26 } };

String columns[] = { "Name", "Age" };

final JPopupMenu popUp = new JPopupMenu("Customized Tool Tip");

final JTable table = new JTable(new DefaultTableModel(rows, columns));
table.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
    int row = table.rowAtPoint(e.getPoint());
    int column = table.columnAtPoint(e.getPoint());

    Rectangle bounds = table.getCellRect(row, column, true);

    popUp.setVisible(false);
    popUp.removeAll();
    popUp.add(new JTextField("Mouse Position - Row : " + row + ", Column : " + column));
    popUp.show(table, bounds.x, bounds.y + bounds.height);
    popUp.setVisible(true);
    }
});

JScrollPane pane = new JScrollPane(table);

frame.add(pane, BorderLayout.CENTER);

frame.setSize(300, 150);
frame.setVisible(true);
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
2

Just use html lable in tooltip like this :

table.setToolTipText("<html>"
    + "Get pic from http://www.jfree.org/jfreechart/images/CrossHairDemo2-254.png<br><br>"
    + "<img src=\"http://www.jfree.org/jfreechart/images/CrossHairDemo2-254.png\"></img></html>");

enter image description here

Terrence
  • 116
  • 7