在JTable中鼠标悬停创建信息面板? 提示可能不够(Create info panel on

2019-07-30 11:03发布

我想在鼠标移动到使用Java Swing的 JTable中单元格中显示一个信息框,所以存在多件

  1. 我怎样才能捕捉表格单元格鼠标悬停事件? 我必须能够设置单元格的内容,然后得到它的数据。
  2. 我怎么能在鼠标悬停在该单元格中显示动态服务器数据面板/盒?
  3. 我怎样才能缓存信息板/盒,所以我没有来查询每个鼠标的服务器上?

例:

在表格单元格我输入:94903. Tab键或进入后,该单元被设置成数。 在鼠标悬停时,它显示有姓名,地址,电话号码,电子邮件等一箱

谢谢!

Answer 1:

你可以格式化使用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的的答案弹出菜单以及



Answer 2:

下面有一个很好的办法让鼠标在工作我woud为此,首先那么就输出一些文字,当你在它进行检查。

这显示了一种方式来获得一个弹出菜单出现

而在关于该cacheing我可能最后10个值每一个新的入口点ping通时间,即你没有在本地存储在内存中,从服务器上执行的请求。 然后也许每分钟更新的最后10命中柜面他们的信息变化。

另一种有用的鼠标悬停指南



文章来源: Create info panel on mouseover in JTable? Tooltip might not be sufficient