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

后端 未结 6 776
忘掉有多难
忘掉有多难 2020-12-09 10:42

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:

6条回答
  •  执笔经年
    2020-12-09 11:31

    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();
    }
    

提交回复
热议问题