tags:

views:

67

answers:

3

I want to use that class and not call on a JComponent setToolTipText method but the code below not show the tooltip:

JButton btn = new JButtn("SAVE");
JToolTip tip_for_button = new JToolTip();
tip_for_button.setTipText("blah blah");
tip_for_button.setComponent(btn);

why?

+1  A: 

You probably need to call setToolTipText(). See http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/tooltip.html for details.

Though he states he doesn't want to use "setToolTipText()".
Gnoupi
A: 

What is tip_for_button? For it to work I am pretty sure that tip_for_button needs to be tip

steven_h
Yes sorry I changed the code but, this is not the problem.
xdevel2000
+1  A: 

Well, because all the code that actually catches the mouse event and shows the tooltip is in ToolTipManager (the instance of this class is singleton, unique in the application), and ToolTipManager always calls JComponent.createToolTip() method on component when determining what tooltip to show. So if you want to use your own tooltip you have to override this method and write something like this:

JButton btn = new JButton("SAVE"){

        public JToolTip createToolTip() {
           JToolTip tip_for_button = new JToolTip(){
               public String getTipText() {
                   return "blah blah";
               }
           };
           tip_for_button.setComponent(this);
           return tip_for_button;
       }
       };
       btn.setToolTipText("notnull");

setToolTipText is mandatory, or the tooltip will not show, and the text passed to it is always set to the tooltip created by Component, so if you want the immutable text, you override JToolTip.getTipText().

Taisin
Hum, I tried this, but it does not work... are you sure it works for you ?
Sylvain M
@Sylvain M corrected code, now it does work.
Taisin
Thx, by reading the API and making tries, my conclusion was also that without calling setToolTipText(), no tooltip is displayed. I don't know if we're right, but for the moment, only this solution seems to work...
Sylvain M
@Sylvain M Yes, we are right. In the source code of ToolTipManager in the class that manages the showing of the tooltip when mouse enters the component ($insideTimerAction) there is this code (I cut out the irrelevant lines): toolTipText = insideComponent.getToolTipText(mouseEvent); if(toolTipText != null) { showImmediately = true; showTipWindow(); }else{hideTipWindow();}
Taisin
However if we remove btn.setToolTipText("notnull") and put ToolTipManager toolTipManager = ToolTipManager.sharedInstance();toolTipManager.registerComponent(btn);the tooltip show only with CTRL+F1 but not with mouse over the component!!!! Strange Strange
xdevel2000
@xdevel2000 as I've said, in the code ToolTipManager$insideTimerAction the getToolTipText is checked to be not null, and this class manages the tooltips for mouse over event. I really don't understand why JToolTip.getText isn't checked instead, or why getToolTipText isn't checked in other methods, but well.
Taisin