在JTable中创建鼠标hover信息面板? 工具提示可能还不够

我想显示一个使用Java Swing鼠标hover在JTable单元格上的信息框,因此有多个部分

  1. 如何捕获表格单元格中的鼠标hover事件? 我必须能够设置单元格内容,然后获取数据。
  2. 如何在鼠标hover在该单元格上时显示包含动态服务器数据的面板/框?
  3. 如何缓存信息面板/框,以便我不必在每个鼠标上查询服务器?

例:

在表格单元格中输入:94903。在选项卡或输入后,单元格设置为数字。 在鼠标hover时,它会显示一个包含姓名,地址,电话号码,电子邮件等的框。

谢谢!

您可以使用HTML格式化工具提示文本,这将允许您向工具提示提供复杂的信息结构,而无需编写自己的解决方案。 唯一的问题是工具提示将被自动丢弃。

如果这仍然不合适,你可以尝试:

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

现在,我还没有构建弹出窗口,我也使用Bob Sinclar的答案中的弹出菜单

这是一个让鼠标过度工作的好方法我首先做这个,然后只需输出一些文本就可以检查。

这显示了一种显示弹出菜单的方法

关于缓存,我可能会将最后10个值存储在内存中,并在每次执行新的入口点时从服务器执行请求,即您没有在本地使用它。 然后可能每一分钟更新最后10次点击,因为他们的信息会发生变化。

另一个有用的鼠标指南