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:
JMultiLineToolTip
is a good one to use?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?
If you are not afraid of extending swing tool tip, you can create your own JMultiLineToolTip:
paint()
method to write given string in multi lineHere is an example - it shows how to use it as well
However, to answer your questions:
Which JMultiLineToolTip is a good one to use?
Use <html>
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>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With