Long lines in Java Tooltips (or multiline tooltips)

Do you want to show a tooltip with more than one line? Are your tooltips too large to fit in one line? The solution is easier than you think. Tooltips, in Java, accept HTML code:

jcomponent.setToolTipText("HTML tooltip");

It isn’t a Firefox, but is very powerful. And, when I said HTML, I also think about CSS. So, you can set your tooltips using this evil cheat:

<html><body>
<div style=”width: 300px; text-justification: justify;”>
Blah Blah Blah (repeat “blah” 100 times)
</div>
</html></body>

Have you seen automatic line breaks and the justification? Oh, yeah!

BTW, do you remember old days, when you couldn’t believe WordPad didn’t have justified text? :)

Tags: , , , ,

4 Responses to “Long lines in Java Tooltips (or multiline tooltips)”

  1. edu Windows Vista Internet Explorer 7.0 Says:

    Hi, i found out what you’re saing but i need to chage the container of the tooltip to an other component (not JPanel). Is it posible, and how?
    Thanks.
    PD: I’m a NetBeans adict too !
    PD: Do you speak spanish ?

  2. edu Windows Vista Internet Explorer 7.0 Says:

    Hi!
    I’m having trouble with some tooltips too, and i found out reading the source code of the java jars (via NetBeans 6.1) the same as you. I have a lot of questions on this (how to show an image is the first one…)
    I’m trying to override the container (the JPanel that renders the HTML) and cant find the starting point, where to start. Can ypu help me out?
    Thanks a lot.
    PD: Do you speak spanish ?
    Bye.

  3. Eduardo Costa Linux Mozilla Firefox 3.0 Says:

    Edu,

    I’ve never tried it, but you can override JComponent.createToolTip (you will need to create a child class of JToolTip).

    Regards,
    Eduardo

  4. edu Windows Vista Internet Explorer 7.0 Says:

    I’ve done this already, but the main problem is that i want a transparent background for my tooltip. It works half a way. When the ToolTipo container (JPannel) shows inside the tooltiped control’s container it is transparent (setting setOpaque to false) but when it shows partially of full outside it gets opaque ?? How is this possible, and what can i don for changinng this behavior ?
    Thanks.
    Edu

Leave a Reply