Create dashboard on mouse over in JTable? Hint may not be enough

I want to display a message box on the mousing over the cell the JTable , using the Java the Swing , therefore, there are a few parts

  • How can I capture the mouse-over event in a table cell? I should be able to set the contents of the cell and then receive the data.
  • How can I display a panel / field with dynamic server data on mousing above this cell?
  • How can I cache the dashboard / field, so I don’t need to query the server on each mouse?

Example:

In the cell of the table I enter: 94903. After tabulation or input, the cell is set to a number. When you hover the mouse, a field with a name, address, phone number, email, etc. is displayed.

Thank!

+2
source share
2 answers

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

If this still does not fit, you can 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());
        showTimer.setRepeats(false);
        showTimer.setCoalesce(true);

        disposeTimer = new Timer(5000, new DisposePopupActionHandler());
        disposeTimer.setRepeats(false);
        disposeTimer.setCoalesce(true);

        table.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            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

                        showTimer.restart();

                    }

                }

            }
        });

    }

    protected MyPopup getHintPopup() {

        if (popup == null) {

            // Construct the popup...

        }

        return popup;

    }

    public class ShowPopupActionHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (hintCell != null) {

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

                MyPopup popup = getHintPopup();
                popup.setVisible(false);

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

                disposeTimer.start();

            }

        }
    }

    public class DisposePopupActionHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            MyPopup popup = getHintPopup();
            popup.setVisible(false);

        }
    }
}

Now I did not create a popup, I would also use the popup menu from Bob Sinclar's answer

+4
source

All Articles