[英]JTable ToolTip as a JComponent or JPanel
I'm using a JTable (specifically a JXTable ) and when I hover over a cell I would like to present some sort of JPanel or Component (to show some graphical data using JFreeChart) 我使用的是JTable(特别是JXTable ),当我将鼠标悬停在单元格上时,我想呈现某种JPanel或Component(使用JFreeChart显示一些图形数据)
I know the JTable has this: 我知道JTable有这个:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getToolTipText(java.awt.event.MouseEvent) http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getToolTipText(java.awt.event.MouseEvent)
but that is obviously just a way to show a textual tooltip. 但这显然只是显示文本工具提示的一种方式。 Any ideas or solutions to do this? 有什么想法或解决方案吗?
You can show a pop up on mouse over and you can customize the pop up window however you want. 您可以在鼠标悬停时显示弹出窗口,也可以根据需要自定义弹出窗口。 I hope the below piece of code would help. 我希望下面的代码会有所帮助。
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);
Just use html lable in tooltip like this : 像这样在工具提示中使用html标签:
table.setToolTipText("<html>"
+ "Get pic from http://www.jfree.org/jfreechart/images/CrossHairDemo2-254.png<br><br>"
+ "<img src=\"http://www.jfree.org/jfreechart/images/CrossHairDemo2-254.png\"></img></html>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.