1

I've tried to implement a resizeable tooltip but have some problems with this.

When the tooltip is shown, the text updates on change in the model (implemented with property change listener). What I need now is, that the tooltip changes its size as well depending on the text.

Tried revalidate, doLayout and repaint but with no effect. The tooltip doesn't change its size. Only moving the mouse gives me a correct sizing for the first text which is displayed in the tooltip.

Can anybody help?

Here are some code snippets: first of all my tolltip class:

public class ResizeableToolTip extends JToolTip {

    public ResizeableToolTip(final JComponent component) {
        super(component);
        initComponents();
    }

    @Override
    protected void initGUI() {
        super.initGUI();
        setLayout(new BorderLayout());
    }

    /**
    * Component initialization goes here...
     */
    private void initComponents() {
        setTipText(getComponent().getToolTipText());
        setPreferredSize(calculateOptimalSize());
    }

    private abstract Dimension calculateOptimalSize();

    @Override
    public void setTipText(String tipText) {
        super.setTipText(tipText);
        setPreferredSize(calculateOptimalSize());
        revalidate();
    }
}

Then I have a button which is using this implementation:

public class MyButton extends JButton implements PropertyChangeListener {
    //...
    private ResizeableToolTip tooltip;
    //...
    private initComponents() {
        //...
        tooltip = new ResizeableToolTip(this);
        //...
    }
    //...
    public void propertyChange(final PropertyChangeEvent pcevt) {
        //...
        if (MyButtonModel.TOOLTIPTEXT_PROPERTY.equals(pcevt.getPropertyName()) {
            tooltip.setTiptext((String) pcevt.getNewValue());
            tooltip.repaint();
        }
        //...
    }
    //...
}

The result should be a tooltip which is displayed over the button where the text is changing when s.th. changes in the data model. The text changes are working but the size of the box of the tooltip stays on the wrong size.

MikeO
  • 269
  • 1
  • 6
  • 13
  • Could you share a code or at least an image which would describe you problem better? I am not sure if I get your problem. Is it in anyway related [to HTML use in `JLabel` or tool-tip](http://stackoverflow.com/q/11033800/613495)? – Boro Jun 14 '12 at 14:46
  • Performing layout in a JToolTip is a bit unusual, [see here](http://stackoverflow.com/questions/9522475/custom-java-tool-tip-with-swing-components-as-content-does-not-show-up) I'd just use the regular JToolTip and set some html text into it. It will re-size itself for the given html. You may also have luck with the [Ballon tip project](http://balloontip.java.net/) – Enwired Jun 14 '12 at 17:09
  • Tried that but it doesn't resize itself. Only when the tooltip is visible, it is sized depending on the text you have in the tooltip. If the the text changes in the timeslice the tooltip is displayed, the tooltip stays on his size, even the text needs more space :-(, – MikeO Jun 18 '12 at 13:09

1 Answers1

0

As far as i can tell there is not way to resize it besides moving the mouse. However the component should have one mouse mouse motion listener so all you have to do is call mouseMoved on that and it will think the mouse has been moved and will resize the tooltip.

//comp will be whatever component your tooltip is on
if(comp.getMouseMotionListeners().length > 0)comp.getMouseMotionListeners()[0].mouseMoved(new MouseEvent(inst, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y, 0, false));

Just call that after you set the tooltip and it should resize.