Create info panel on mouseover in JTable? Tooltip

2019-04-02 06:44发布

问题:

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?

Example:

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.

Thanks!

回答1:

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());
        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've not constructed the popup, I'd use the popup menu from Bob Sinclar's answer as well



回答2:

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