我正在使用 JTable(特别是 JXTable ),当我将鼠标悬停在单元格上时,我想呈现某种 JPanel 或组件(以使用 JFreeChart 显示一些图形数据)
我知道 JTable 有这个:
但这显然只是显示文本工具提示的一种方式。有什么想法或解决方案可以做到这一点吗?
最佳答案
您可以在鼠标悬停时显示弹出窗口,并且可以根据需要自定义弹出窗口。我希望下面的代码会对您有所帮助。
JFrame frame = new JFrame("Sort Records based on Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = { { "Loganathan Mohanraj", 31 },
{ "Renuka Loganathan", 26 } };
String columns[] = { "Name", "Age" };
final JPopupMenu popUp = new JPopupMenu("Customized Tool Tip");
final JTable table = new JTable(new DefaultTableModel(rows, columns));
table.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
Rectangle bounds = table.getCellRect(row, column, true);
popUp.setVisible(false);
popUp.removeAll();
popUp.add(new JTextField("Mouse Position - Row : " + row + ", Column : " + column));
popUp.show(table, bounds.x, bounds.y + bounds.height);
popUp.setVisible(true);
}
});
JScrollPane pane = new JScrollPane(table);
frame.add(pane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
关于java - 作为 JComponent 或 JPanel 的 JTable 工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25438803/