Re: Tooltip location outside of JComponenet
Le 17/11/2009 22:50, Christian Kaufhold a ??crit :
> [snip]
Sorry but your code didn't work. When scrolling the tooltip didn't change.
The problem come from TooltipManager because it save the MouseEvent with
the mouse coords of "before the show delay".
I've modified your code and now it works:
==============================================
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.*;
import java.awt.*;
public class TableToolTip
{
public static void main(String[] args)
{
JTable t = new JTable(20, 8)
{
private int rowTT = -1;
private int columnTT = -1;
private Point lastLoc;
public String getToolTipText(MouseEvent event) {
// the MouseEvent is old, use the new mouse coords
Point screenLoc= MouseInfo.getPointerInfo().getLocation();
Point p = new Point(screenLoc);
SwingUtilities.convertPointFromScreen(p, this);
MouseEvent newEvent = new MouseEvent(event.getComponent(),
event.getID(),
event.getWhen(), event.getModifiers(),
p.x, p.y,
screenLoc.x,
screenLoc.y,
event.getClickCount(),
event.isPopupTrigger(),
MouseEvent.NOBUTTON);
// JTable.getToolTipText(...) use the renderers
return super.getToolTipText(newEvent);
}
public Point getToolTipLocation(MouseEvent e) {
// the MouseEvent is old, use the new mouse coords
Point location= MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, this);
// see ToolTipManager.showTipWindow(...)
location.y += 20;
int row = rowAtPoint(location);
int column = columnAtPoint(location);
if (row < 0 || column < 0)
return null;
// the cell is not the same, re-show
if (row != rowTT || column != columnTT) {
rowTT = row;
columnTT = column;
lastLoc = location;
return location;
}
// if same cell, same loc as previous call
return lastLoc;
}
public Object getValueAt(int row, int column) {
return "("+row+","+column+")";
}
};
t.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
setToolTipText("TT = " + value.toString());
return this;
}
});
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(t));
f.setBounds(200, 200, 400, 400);
f.setVisible(true);
}
}