Link to home
Start Free TrialLog in
Avatar of glottis
glottisFlag for Pakistan

asked on

Custom ToolTips

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.
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

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
Avatar of glottis

ASKER

umm.... explanation please :)
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
Avatar of Webstorm
Webstorm

Hi glottis,

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

Avatar of glottis

ASKER

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).
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.

Avatar of glottis

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of johnknapp
johnknapp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of glottis

ASKER

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.