当光标指向特定行时,如何获取JTable中的行和列?

我正在创建一个包含基本数据的表,这是我的框架 在此处输入图像描述

当我点击该行时,它将弹出一个包含所有数据的新帧,它运行良好,当我点击一行时,我可以正确获取行和列,

在此处输入图像描述

但这是我想要做的:

当我将光标设置在表格行上时,它会自动获取表格中该数据的行和列,并且我想显示该数据的图片,就像在fb上一样,当您将光标设置在配置文件上时, 2秒后,它将显示该用户的个人资料。 请帮助,我只是一个新手:)

您可以做的是覆盖JTablehttps://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/prepareRendererhttps://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/方法并为每个单元格设置工具提示。 然后使用一些html作为工具提示,如AndrewThompson的答案所示

我从这个网站(这个问题 )使用此图片这个urlhttp://i.stack.imgur.com/Bbnyg.jpghttps://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/ ,但您可能希望使用系统或类路径中的资源并使用toUri().toUrl()https://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/创建url。 在任何情况下,html都需要包含中的URL。 您可以根据row/columnhttps://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/值切换它们。

在此处输入图像描述

这是一个例子。

在此处输入图像描述

 import java.awt.Component; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; public class TestTableTooltip { String html = "" + " "; public TestTableTooltip() { String[] cols = {"COL", "COL", "COL"}; String[][] data = { {"Hello", "Hello", "Hello"}, {"Hello", "Hello", "Hello"}, {"Hello", "Hello", "Hello"} }; DefaultTableModel model = new DefaultTableModel(data, cols); JTable table = new JTable(model) { public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); if (c instanceof JComponent) { JComponent jc = (JComponent) c; jc.setToolTipText(html + "
" + getValueAt(row, column).toString() + ": row, col (" + row + ", " + column + ")" + ""); } return c; } }; JFrame frame = new JFrame(); frame.add(new JScrollPane(table)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TestTableTooltip(); } }); } }https://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/

UPDATE

您可以从资源(在类路径中)获取这样的URL

  URL url = getClass().getResource("/path/to/image.png"); final String html = "" + " ";https://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/ 

如果它是文件系统中的文件,则可以执行此操作

  URL url = new File("path/to/image.png").toURI().toURL();https://stackoverflow.com/questions/22245463/how-to-get-the-row-and-the-column-in-a-jtable-when-the-cursor-is-pointed-at-a-sp/