Solved

Custom ToolTips

Posted on 2004-04-13
9
699 Views
Last Modified: 2008-03-17
What I want is that no matter what component is used, the tooltip which is shown (if it is shown) then it should be my own custom made.
for example I want the background color to be Color.ABC and the the font to be be Font.XYZ.

The default tooltip Look and feel should be this, no matter what LnF is installed.
0
Comment
Question by:glottis
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
Use getter methods for your dynamic text.

In the setToolTipText method call that getter methods.  
That is the only way to generate the dynamic tooltip irrespective of LnF.


Regards,
Muruga
0
 
LVL 2

Author Comment

by:glottis
Comment Utility
umm.... explanation please :)
0
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
the thing is,

Say, you want to display a tool tip with the text field value.

Then you have to use, setToolTipText(text.getText());

If you want to display the background color of a component then

setToolTipText(component.getBackground().toString());

Same way, you can have your own getter method to populate the tooltip value.


Regards,
Muruga
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
Hi glottis,

Try this :
  JToolTip jtt = component.createToolTip();
  jtt.setBackground(Color. ...);
  jtt.setFont(new Font( ... ));

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:glottis
Comment Utility
mmuruganandam: youve got me wrong, i donot want to display dynamic content. What I want is that by default all the tooltips which popup should have its properties lets say:

Font = "Times New Roman", Font.PLAIN, 12
Background Color = Color.BLACK
Foreground Color = Color.WHITE

Webstorm:
Currently wha i have done is that I have extended every component and overiden the createToolTip method, but that is a tedious job.
same way your solution would make me re write my entire code so that i may change the tooltip of every component (I have a lot of components).
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
Just do this:

    JToolTip getToolTip(JComponent jcomp)
    {
         JToolTip jtt = jcomp.createToolTip();
         jtt.setBackground(Color. ...);
         jtt.setFont(new Font( ... ));
         return jtt;
    }

And call it for each component.

Or (better solution) you can create a new look & feel and extend the BasicToolTipUI class.

0
 
LVL 2

Author Comment

by:glottis
Comment Utility
Webstorm:
that is what I did, and for some reason (i dont know what) JComboBox was not behaving correctly :)

other components were showing the proper tool tip, but not JComboBox.

One last solution would be to user new LnF.... but still is there any other way ?
0
 

Accepted Solution

by:
johnknapp earned 20 total points
Comment Utility
When it comes to tool tips why don't you just set the tool tip text to an html string, it won't do much good for anything crazily complicated but does provide an easy way to format your tool tips no matter what LNF is installed.

<code>
button.setToolTipText("<html><body bgcolor=\"#345243\"><font color=\"#FFFFFF\" style=\"bold\" face=\"serif\"><b>I am telling you to click me!</b></font></body></html>");
</code>

You could also create a wrapper method that wraps text in some arbitrary html format possibly defined in a props file for on the fly changes and then just code that into your components by overriding the setToolTipText(String s)

ex.

public class MyComp extends JComponent {

    public void setToolTipText(String text) { // text here being just a plain old unformatted string
       super.setToolTipText(SomeStaticUtilityClass.getHtmlWrapper(text)); // getHtmlWrapper(String s) defined in some static utility class
    }

}

or just call it externally on all your components

myButton.setToolTipText(SomeStaticUtilityClass.getHtmlWrapper(text));

Another way is to just go with what webstorm said with the javax.swing.plaf.basic.BasicToolTipUI

Also, the combo box is a strange hybrid component.  Is The combo in question editable?, did you override the renderer for it? or the editor? is it heavy weight or light weight?

If you have extended JComboBox then make sure many of JComboBox methods (such as createToolTip()) route to its renderer if non editable or editor if editable, I had a similar problem when I wrote a AutoSelectComboBox with the focus listener registration methods.  

0
 
LVL 2

Author Comment

by:glottis
Comment Utility
johnknapp:
thanks for hte help.

i will look into this.

My combox box is editable, and what I did was:

public class MyCombo extends JComboBox {
 ...
 public JToolTip createToolTip() {
  ...
 }
}

however this method was never called. I think I should better set the tooltip on the editor, and the renderer.

Thanks again.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now