Hello,
I found code in the forum (
9697832 for the above mentioned task. The posting is of this year, but when I try to apply the code, my toolTipAction is always null. Is there a mistake in my code or a change in jdk7?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TTTest extends JFrame {
public TTTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp= getContentPane();
cp.setLayout(null);
setSize(260,300);
final JTextField tf= new JTextField();
tf.setToolTipText("This is the tooltip.");
tf.setBounds(75,80,100,25);
JButton b= new JButton("Display tooltip");
b.setBounds(50,150,150,30);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Action toolTipAction = tf.getActionMap().get("postTip");
if (toolTipAction != null) {
ActionEvent postTip = new ActionEvent(tf, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);
}
else {
// Since toolTipAction is null, let's inspect the map
ActionMap map = tf.getActionMap();
Object[] keys = map.allKeys();
for (Object o: keys) {
// System.out.println(o); // Indeed, there's no "postTip" key.
if (o.equals("postTip")) System.out.println("--- KEY FOUND! ---");
}
}
}
});
cp.add(tf);
cp.add(b);
setVisible(true);
}
static public void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TTTest();
}
});
}