问题描述
我有 JTable
。一列包含 JPanel
,其中包含一些 JLabels
,其中包含 ImageIcons
。我创建了一个自定义单元格渲染,除了 JLabel
上的工具提示外,所有工作都正常。当我将鼠标悬停在任何 JLabels
上时,我需要显示该特定 JLabel的
。它没有显示工具提示
JLabel
的tootlip。
I have a JTable
. One column holds a JPanel
which contains some JLabels
with ImageIcons
. I have created a custom cell render and all works fine apart from the tool tip on the JLabel
. When I mouse over any of these JLabels
I need to show the Tooltip
of that particular JLabel
. Its not showing the tootlip of the JLabel
.
这是 CustomRenderer
。
private class CustomRenderer extends
DefaultTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (value != null && value instanceof List) {
JPanel iconsPanel = new JPanel(new GridBagLayout());
List<ImageIcon> iconList = (List<ImageIcon>) value;
int xPos = 0;
for (ImageIcon icon : iconList) {
JLabel iconLabel = new JLabel(icon);
iconLabel.setToolTipText(icon.getDescription());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 1;
gbc.gridx = xPos++;
iconsPanel.add(iconLabel, gbc);
}
iconsPanel.setBackground(isSelected ? table
.getSelectionBackground() : table.getBackground());
this.setVerticalAlignment(CENTER);
return iconsPanel;
}
return this;
}
}
推荐答案
问题是您在CellRenderer返回的组件的子组件上设置工具提示。要执行您想要的操作,您应该考虑在JTable上覆盖 getToolTipText(MouseEvent e)
。从事件中,您可以找到鼠标所在的行和列,使用:
The problem is that you set tooltips on subcomponents of the component returned by your CellRenderer. To perform what you want, you should consider override getToolTipText(MouseEvent e)
on the JTable. From the event, you can find on which row and column the mouse is, using:
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
然后你可以重新准备单元格渲染器,找到哪个组件位于鼠标位置并最终检索其工具提示。
From there you could then re-prepare the cell renderer, find which component is located at the mouse position and eventually retrieve its tooltip.
这是一个如何覆盖JTable getToolTipText的片段:
Here is a snippet of how you could override JTable getToolTipText:
@Override
public String getToolTipText(MouseEvent event) {
String tip = null;
Point p = event.getPoint();
// Locate the renderer under the event location
int hitColumnIndex = columnAtPoint(p);
int hitRowIndex = rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1) {
TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex);
Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
component.setBounds(cellRect);
component.validate();
component.doLayout();
p.translate(-cellRect.x, -cellRect.y);
Component comp = component.getComponentAt(p);
if (comp instanceof JComponent) {
return ((JComponent) comp).getToolTipText();
}
}
// No tip from the renderer get our own tip
if (tip == null) {
tip = getToolTipText();
}
return tip;
}
这篇关于JTable中JPanel中的工具提示不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!