将 Swing 组件作为内容的自定义 Java 工具提示不显示 [英] Custom Java tool tip with Swing components as content does not show up

查看:26
本文介绍了将 Swing 组件作为内容的自定义 Java 工具提示不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在组件的工具提示中显示多个图像,找到了 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

  • 不是被设计为容器,它只是偶然的容器,因为 JComponent 是.对于 Swing 非容器",它的 ui 委托有责任充当 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆