Solved

Custom ToolTips

Posted on 2004-04-13
9
704 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 85
What's wrong with this code? 4 44
Html split(text) 2 44
Read CLOB data from Oracle using JAVA 3 34
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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…
Suggested Courses

738 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