8

Possible Duplicate:
Multi-line tooltips in Java?

It is very strange. All I wish to do is make my tool tip multi-lined. I have added the "\n" character to the string I am passing at appropriate places. In fact, I print out that same string, and it does have the line breaks. However, the tooltip does not. Here is what I do:

        @Override
        public void itemStateChanged(ItemEvent arg0) {
            if(arg0.getStateChange() == ItemEvent.SELECTED){
                String s = arg0.getItem().toString();
                for(InfoContainer i: mc.myInfo)
                    if(s.equals(i.getId())){
                        selector.setToolTipText(i.getInfo());
                        System.out.println(i.getInfo());
                        return;
                    }
            }
        }

However, the tooltip does NOT have the carriage returns, while the System printout DOES.

Community
  • 1
  • 1
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • See: http://stackoverflow.com/questions/868651/multi-line-tooltips-in-java – MicSim Jul 13 '11 at 21:03
  • [You can use basic HTML][1] to format your Tooltip-Text. [1]: http://stackoverflow.com/questions/868651/multi-line-tooltips-in-java – Lukas Knuth Jul 13 '11 at 21:03

2 Answers2

11

How about using: "<html>" + firstLine + "<br>" + secondLine + "</html>"

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Don't forget to escape the content of `firstLine` and `secondLine`. Imagine what would happen when they contain '<' or '>' for example. – gouessej Sep 06 '21 at 08:10
6

Use the HTML tag <br/>:

selector.setToolTipText("<html>" + i.getInfo() + "<br/>some text next line</html>" );
MByD
  • 135,866
  • 28
  • 264
  • 277