mainstringargs
mainstringargs

Reputation: 14381

JTable ToolTip as a JComponent or JPanel

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?

Upvotes: 0

Views: 562

Answers (2)

Terrence
Terrence

Reputation: 116

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

Upvotes: 2

Loganathan Mohanraj
Loganathan Mohanraj

Reputation: 1874

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

Upvotes: 2

Related Questions