Take the 2-minute tour ×

I want to display an info box on mousing over a JTable cell using Java Swing, so there are multi-parts

  1. How can I capture the mouse-over event in a table cell? I have to be able to set the cell content, then get data on it.
  2. How can I display a panel/box with dynamic server data on mousing over that cell?
  3. How can I cache the info panel/box so I don't have to query the server on every mouse over?


In table cell I enter: 94903. After tabbing or entering, the cell is set to the number. On mouse-over, it displays a box with Name, Address, Phone number, email, etc.


share|improve this question
Please read the faq, this will help you to solve your problems. Because this way you are loosing only time. –  Vash Jul 17 '12 at 21:30
not sure what you mean by that. –  Ninjack Jul 17 '12 at 22:15
thanks for formatting though. –  Ninjack Jul 17 '12 at 22:16
TablePopupEditor is an example. –  trashgod Jul 18 '12 at 2:10
@Ninjack, One question per question, for example. –  Vash Jul 18 '12 at 9:03
add comment

2 Answers

up vote 3 down vote accepted

You could format the tooltip text using HTML, this would allow you to provide a complex structure of information to the tooltip without the need or expense of writing your own solution. The only problem is that the tooltip will be automatically discarded.

If this still doesn't suit, you could try:

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.Timer;

public class TestTable {

    private Timer showTimer;
    private Timer disposeTimer;
    private JTable table;
    private Point hintCell;
    private MyPopup popup; // Inherites from JPopupMenu

    public TestTable() {

        showTimer = new Timer(1500, new ShowPopupActionHandler());

        disposeTimer = new Timer(5000, new DisposePopupActionHandler());

        table.addMouseMotionListener(new MouseMotionAdapter() {

            public void mouseMoved(MouseEvent e) {

                Point p = e.getPoint();
                int row = table.rowAtPoint(p);
                int col = table.columnAtPoint(p);

                if ((row > -1 && row < table.getRowCount()) && (col > -1 && col < table.getColumnCount())) {

                    if (hintCell == null || (hintCell.x != col || hintCell.y != row)) {

                        hintCell = new Point(col, row);
                        Object value = table.getValueAt(row, col);
                        // Depending on how the data is stored, you may need to load more data
                        // here...
                        // You will probably want to maintain a reference to the object hint data






    protected MyPopup getHintPopup() {

        if (popup == null) {

            // Construct the popup...


        return popup;


    public class ShowPopupActionHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (hintCell != null) {

                disposeTimer.stop(); // don't want it going off while we're setting up

                MyPopup popup = getHintPopup();

                // You might want to check that the object hint data is update and valid...
                Rectangle bounds = table.getCellRect(hintCell.y, hintCell.x, true);
                int x = bounds.x;
                int y = bounds.y + bounds.height;

                popup.show(table, x, y);




    public class DisposePopupActionHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            MyPopup popup = getHintPopup();


Now, I've not constructed the popup, I'd use the popup menu from Bob Sinclar's answer as well

share|improve this answer
thanks, this is almost exactly what i needed. –  Ninjack Jul 18 '12 at 0:20
A little vote up would go along way Hint, hint, nudge, nudge, say no more, say no more ;) –  MadProgrammer Jul 18 '12 at 1:20
+1 for being blunt :) –  Bob Sinclar Jul 18 '12 at 3:13
+1. Should the line be: if (hintCell == null || (hintCell.x !=*row*|| hintCell.y != col)) ? –  Ninjack Jul 18 '12 at 17:00
No, I'm pretty sure x represents the horizontal (column) & y represents the vertical (row) –  MadProgrammer Jul 18 '12 at 19:54
add comment

Heres a good way to get the mouse over working I woud do this first then just output some text when your over it to check.

This shows a way to get a pop up menu to appear

And in regards to the cacheing i might store the last 10 values in memory and do a request from the server each time a new entry point is pinged ie you dont have it locally. And then maybe every minute update the last 10 hit incase their info changes.

another useful mouseover guide

share|improve this answer
thanks, the popup menu looks promising. –  Ninjack Jul 17 '12 at 22:15
add comment

Your Answer


