I have made a small example to illustrate my problem.

I have overwritten the createToolTip method for a custom JComponent (eventually it will depend on where abouts the mouse is in the component as to what text to bring up) At first I thought it wasn't being called, but i added the colour changing and it is.

The tooltip text doesn't change from "testTip" but the colours do change. I added the println and the output is:

null
something else

so the text is null, i'm setting it, but all that is ignored because the tooltip actually shows "testTip"

I also tried adding

public String getToolTipText()
{
	return "woop";
}

but this causes there to be no tooltip at all!

Here's my example program, any help is appreciated:

import java.awt.*;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToolTip;

public class Test extends JComponent
{
	
	public Test()
	{
		setToolTipText("testTip");
	}
	
	public JToolTip createToolTip() {
		JToolTip tip = super.createToolTip();
		System.out.println(tip.getTipText());
		tip.setBackground(Color.YELLOW);
		tip.setForeground(Color.RED);
		tip.setTipText("something else");
		tip.setToolTipText("Something really else");
		System.out.println(tip.getTipText());
		return tip;
	}
	
	public static void main(String[] args)
	{
		Test t = new Test();
		JFrame f = new JFrame("test");
		f.add(t);
		f.pack();
		f.setVisible(true);
    }
}

well, in the end I solved all my problems by adding a mouse event!! With this I can effect the text based on the location without having to overide the createToolTip event

public String getToolTipText(MouseEvent e)
{
	return "woop";
}

Don't know why there is a dud getToolTipText() that never gets called

Just had a quick look at the source code for JComponent and JToolTip...
setToolText just records the text as a clientProperty of the JComnponent - it doesn't seem to create or initialise the tooltip at that point. setTipText seems to directly set an instance variable in the tooltip itself.
Maybe (this is a guess) the first time the tooltip needs to be displayed createToolTip gets called to create the tooltip (and your overridden version sets the text, although the inherited version doesn't) then the text is set from the JComponent's clientProperty. Maybe.
I think you should be doing this by creating a custom subclass of JToolTip that displays how you want, and overriding the JComponent's createToolTip() to return an instance of your own subclass.

Edit: parallel post with your second post. I was going to suggest that if my theory was right then your method should work when you call it a second and subsequent time... maybe that's what the house listener code is achieving.

Just had a quick look at the source code for JComponent and JToolTip...
setToolText just records the text as a clientProperty of the JComnponent - it doesn't seem to create or initialise the tooltip at that point. setTipText seems to directly set an instance variable in the tooltip itself.
Maybe (this is a guess) the first time the tooltip needs to be displayed createToolTip gets called to create the tooltip (and your overridden version sets the text, although the inherited version doesn't) then the text is set from the JComponent's clientProperty. Maybe.
I think you should be doing this by creating a custom subclass of JToolTip that displays how you want, and overriding the JComponent's createToolTip() to return an instance of your own subclass.

Edit: parallel post with your second post. I was going to suggest that if my theory was right then your method should work when you call it a second and subsequent time... maybe that's what the house listener code is achieving.

If I had time I would test it out, thank you. About the clientProperty, a good test of this maybe to register the JComponent with the ToolTipManager by means other than setToolTipText(), this would mean that the clientProperty should'nt be set but the tooltip would appear - I'll try it some day!

ToolTip is stinky dead fish, I think that coming in with same form from Java 1.4, and never changed...,

search for ToolTipManager, can help you in some cases, but if you really need works with that, then only html formatted give you ... (java accepted Html <= 3.2),

look for

ToolTipManager ttm = ToolTipManager.sharedInstance();
ttm.setInitialDelay(0);
ttm.setDismissDelay(10000);

for Button JComponents (JMenu, JButton ...) I'm implemented JDialog (min workAroung) by overode ButtonModel

yes there are lots of worAround for ToolTip, but missed basic...., works only in example forms

ToolTip is very, very EDT sensitive

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.