Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Displaying tooltip programmatically

JörgSep 9 2011 — edited Sep 11 2011
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();
      }
    });
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 9 2011
Added on Sep 9 2011
6 comments
1,914 views