Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Java tool tip with Swing components as content does not show up

I'm trying to show multiple images in a component's tooltip, found createToolTip() and implemented a custom that adds the needed components like this:

setComponent(component);

JPanel images = new JPanel(null);
images.setLayout(new BoxLayout(images, BoxLayout.X_AXIS));
for(ImageIcon icon:myIcons) {
    images.add(new JLabel(icon));
}

JPanel content = new JPanel(new BorderLayout());
content.add(new JLabel(title), BorderLayout.NORTH);
content.add(new JLabel(description));
content.add(images, BorderLayout.SOUTH);

add(content);

However, all I see is a little dot, indicating that the tool tip is shown, but somehow the size is ignored. What do I miss implementing a custom tooltip?

like image 990
Silly Freak Avatar asked Mar 01 '12 19:03

Silly Freak


People also ask

Which method is used to add tooltip text to almost all components of Java Swing?

We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.

Which method is used to set the tool tip text?

Use the setToolTipText method to set up a tool tip for the component.

What is tooltip text in Java Swing?

Tooltips are small windows of text that popup when the user leaves the mouse cursor over a component for a second or two. They are used to explain the functionality of the component. Tooltips are an integral part of Swing components. They can be specified by calling the setToolTipText method as shown below.


2 Answers

Tool tips can render HTML. If you can form URLs to the images (not practical if they are generated in memory but usually doable otherwise), it is an easy matter to write some HTML that will load the images, and use that HTML as the tool tip.


E.G.

MultiIconToolTip

import javax.swing.*;

class MultiIconToolTip {

    public static void main(String[] args) throws Exception {
        final String html =
            "<html><body>" +
            "<img src='" +
            "http://i.stack.imgur.com/OVOg3.jpg" +
            "' width=160 height=120> " +
            "<img src='" +
            "http://i.stack.imgur.com/lxthA.jpg" +
            "' width=160 height=120>" +
            "<p>Look Ma, no hands!";
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JLabel hover = new JLabel("Point at me!");
                hover.setToolTipText(html);
                JOptionPane.showMessageDialog(null, hover);
            }
        });
    }
}
like image 73
Andrew Thompson Avatar answered Sep 20 '22 09:09

Andrew Thompson


The base "problems" are that JToolTip

  • is-not designed as a container, it's only accidentally a container because JComponent is. For a Swing "not-container" its the responsibility of the ui-delegate to act as LayoutManager.
  • isn't rich enough, it can handle text-only (at least with the emergency door html, which is @Andrew's favourite :-)

By-passing those limitations basically is a driving that widget nearly over the edge. A clean solution would roll a new component .. On the other hand, the OP already found the screws to tweak. The only thingy that could be slightly improved is to neither call setXXSize, nor set a custom ui. Instead, make it behave like a container by overriding getXXSize() like:

@Override
public Dimension getPreferredSize() {
    if (getLayout() != null) {
        return getLayout().preferredLayoutSize(this);
    }
    return super.getPreferredSize();
}
like image 24
kleopatra Avatar answered Sep 17 '22 09:09

kleopatra