将鼠标悬停在jtable单元格上时显示图像 [英] show an image when hover over a jtable cell
问题描述
我有一个使用DefaultTableModel创建的JTable,我希望在鼠标悬停在该JTable的特定单元格上时显示图像,并且我需要每个单元格的图像不同。
I have a JTable created with a DefaultTableModel, and I want to show an image when the mouse hovers over a particular cell of that JTable, and I need the image to be different for each cell.
提前致谢
推荐答案
您可以覆盖 prepareRenderer $ c单元格/列/行组件的
JTable
和 setToolTip
的$ c>,并使用一些HTML作为tooltop ,请参见此处和此处
You can override prepareRenderer
of the JTable
and setToolTip
for the cell/column/row component, and use some HTML for the tooltop, as see here and here
对于每个不同的图像,您需要获得一个不同的URL作为的
标签。你需要知道一些基本的HTML。对于类路径中的图像,可以使用 src
img getClass()。getResource()
获取URL。或者您可以使用 File.getUri()。getUrl()
来获取文件系统上的图像文件。上面的链接之一还显示了如何从关系数据库中获取图像。
For each different image you will need to get a different URL as the src
of the img
tag. You'll need to know some basic HTML for this. You can obtain an URL by using getClass().getResource()
for images in the class path. Or you can use File.getUri().getUrl()
to get image files on the file system. One of the links above also shows how you can get images from a relational database.
无论您获取URL的方式是什么,您都将使用它作为<$中的URL c $ c>< img src 标签。类似的东西(来自链接答案):
Whatever way you obtain the URL, you will use it for the URL in the <img src
tag. Something like (from linked answer):
URL url = getClass().getResource("/path/to/image");
String html = "<html><body>"
+ "<img src='"
+ url
+ "' width=150 height=150> ";
jc.setToolTipText(html + "<br/>"
+ getValueAt(row, column).toString()
+ ": row, col (" + row + ", " + column + ")"
+ "</body></html>");
您需要以某种方式组织图像,您可以从模型中的数据以某种方式对应到可用于获取URL的路径。假设您有一个名称作为表中的一段数据,那么您希望能够使用该名称作为获取URL的标识符
You will need to organize your images in some way where you can the data from the model corresponds some way to a path that can used to obtain the URL. Say you have a name as a piece of data in the table, then you want to be able to use that name as an identifier in obtaining the URL
更新以及完整示例
鉴于这是我的以下项目结构
Given this is my following project structure
ProjectRoot
src
resources
mario
Mario.png
Luigi.png
以下作品
import java.awt.Component;
import java.net.URL;
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 TableTooltipDemo {
public TableTooltipDemo() {
JTable table = getTable(getModel());
JFrame frame = new JFrame("Table ToolTip");
frame.add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JTable getTable(DefaultTableModel model) {
JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
if (c instanceof JComponent) {
JComponent jc = (JComponent)c;
String name = getValueAt(row, 0).toString();
String html = getHtml(name);
jc.setToolTipText(html);
}
return c;
}
};
return table;
}
private String getHtml(String name) {
URL url = getClass().getResource("/resources/mario/" + name + ".png");
String html
= "<html><body>"
+ "<img src='"
+ url
+ "' width=150 height=150></body></html>";
return html;
}
private DefaultTableModel getModel() {
String[] cols = { "Name", "Age", "Message" };
Object[][] data = { { "Mario", 32, "I am Mario!" },
{ "Luigi", 32, "I am Luigi!" },
{ "Bowser", 32, "I am Bowser!" },
{ "Princess", 32, "I am Princess!" },
{ "Koopa", 32, "I am Koopa!" } };
DefaultTableModel model = new DefaultTableModel(data, cols);
return model;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableTooltipDemo();
}
});
}
}
这篇关于将鼠标悬停在jtable单元格上时显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!