Solved

Custom ToolTips

Posted on 2004-04-13
9
706 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 13

Expert Comment

by:Webstorm
ID: 10811873
Hi glottis,

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

0
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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