[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Custom ToolTips

Posted on 2004-04-13
9
Medium Priority
?
716 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 60 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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