Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make all tooltips multiline in Java (Swing)

Tooltips in my application can be quite long, therefore I'd like them to have line breaks.

I don't want to use html as I'd prefer to set a (max) width of my tooltips instead and have the line breaks dynamically.

In the accepted answer the this similar question Multi-line tooltips in Java? I read about JMultiLineToolTip. Unfortunately the provided link doesn't work anymore and there are many different JMultiLineToolTip out there. Therefore my two questions:

  1. Which JMultiLineToolTip is a good one to use?
  2. How can I use such a class to represent all of the tooltips in my application?

EDIT: as everyone seems to recommend the use of html, is there a way to define the width of my tooltip in pixels (or some other unit than number of characters) using html?

like image 477
Se Norm Avatar asked Dec 21 '22 00:12

Se Norm


2 Answers

If you are not afraid of extending swing tool tip, you can create your own JMultiLineToolTip:

  1. Extend JTooltip In the extended Tool tip component implementation,
  2. set a custom tool tip UI In customUI implementation
  3. Implementpaint() method to write given string in multi line

Here is an example - it shows how to use it as well

However, to answer your questions:

  1. Which JMultiLineToolTip is a good one to use?

    Use <html>

  2. How can I use such a class to represent all of the tooltips in my application?

    Per compopnent, it is easy but tedious to achieve as you will have to override creatreToolTip() API. But if you want to change it globally, you may:

    (i)Simple way - Register your custom tooltip UI with the UIManager at the beginning of your execution.

    UIManager.put( "ToolTipUI", "SeNormToolTipUI" );

    UIManager.put( "SeNormToolTipUI",Class.forName( multiLineToolTipUIClassName ) );

    (ii) complex way

    You will have to start implementing your own look and feel. In the look and feel implementation, you would provide defaults for ToolTipUI as your UI implementation and then set that look and feel to the application you are running. For instance take a look at the MetalLookAndFeel implementation. You may just extend that part and implement your on lnf.

So, it is better to use <html>

like image 66
ring bearer Avatar answered Jan 03 '23 06:01

ring bearer


1) Html is easiest of ways for plain JToolTip

2) use JWindow(un_decorated JDialog) with JTextArea, better would be JTextPane (supporting stylled text),

  • the disadvantage is you have to manually set window to the Point, you have to manually set for setInitialDelay and setDismissDelay (Swing Timer), setVisible(true/false)

  • the advantage is that you using full manageable top level container with definitions for own parent

3) I use JLabel with Html formatted and stylled text added to the GlassPane, notice easiest alternative is use non_opaque JLayeredPane (Java6) or JLayer (Java7)

like image 30
mKorbel Avatar answered Jan 03 '23 06:01

mKorbel