Java, multi-line tooltip with fixed maximum width using html tag

I want to add a multi-line popup to my swing component. The main solution is to use html tags like this:

label.setToolTipText("<html><p width=\"350px\">" + text + "</p></html>"); 

It works great with long text. But if the text, say, contains only one word, it will also have a fixed width of 350 pixels with a lot of empty space.


Is there any special html property for max-width that will work correctly with the setToolTipText () method? I tried the following solution, but it does not work:

 "<p style=\"max-width: 350px\">" 

Should I calculate the width of the text in pixels and change the opening of the <p> tag if the width is less than 350 pixels?

+6
source share
1 answer

I would suggest the following:

After setting a fixed width, you can get your preferred size using:

 Dimension d = someToolTip.getPreferredSize(); if (d.width < 350) { // Insert code to make tooltip-size smaller, by setting it to d.width; } 
-2
source

Source: https://habr.com/ru/post/958162/


All Articles