Solved

Custom ToolTips

Posted on 2004-04-13
9
700 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
ID: 10811377
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
ID: 10811499
umm.... explanation please :)
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10811518
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
ID: 10811873
Hi glottis,

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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:glottis
ID: 10812165
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
ID: 10812737
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
ID: 10813314
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
ID: 10831458
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
ID: 10831991
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

932 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

11 Experts available now in Live!

Get 1:1 Help Now