使用Swing组件作为内容的自定义Java工具提示不会显示 [英] Custom Java tool tip with Swing components as content does not show up
问题描述
我正在尝试在组件的工具提示中显示多个图像,找到 createToolTip()
并实现一个自定义,添加如下所需的组件:
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?
推荐答案
基本问题是JToolTip
The base "problems" are that JToolTip
- is-not 设计为容器,它只是偶然的容器,因为JComponent是。对于Swingnot-container,ui-delegate有责任充当LayoutManager。
- 不够丰富,它可以处理纯文本(至少使用紧急门html,这是@Andrew的最爱: - )
- 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 :-)
绕过这些限制基本上是推动小部件几乎超越边缘。一个干净的解决方案将滚动一个新的组件..另一方面,OP已经找到螺丝进行调整。唯一可以略微改进的是既不调用setXXSize,也不设置自定义ui。相反,通过覆盖getXXSize()来使其行为像容器一样:
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();
}
这篇关于使用Swing组件作为内容的自定义Java工具提示不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!