JPanel标题为标题的工具提示文本的边框

时间:2013-09-09 06:48:43

标签: java swing jpanel tooltip border

我在setBorder(BorderFactory.createTitledBorder(title))中使用JPanel,以便将其内容分组在一个矩形上方,标题位于其上方。如何为标题设置工具提示文本?

4 个答案:

答案 0 :(得分:5)

一种可能的方法是嵌套组件。由于边框不是组件,因此它们不能有工具提示,但是您可以拥有一个仅用于保持边框和工具提示的组件:

JPanel outer = new JPanel();
outer.setBorder(BorderFactory.createTitledBorder("Title"));
outer.setToolTipText("sample text");
JPanel inner = new JPanel();
outer.add(inner);

然后使用inner作为要分组的组件的容器。

答案 1 :(得分:4)

您可以覆盖面板的getToolTipText()方法,检查鼠标是否覆盖标题文字:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitledBorderTest
{
    private static void createAndShowUI()
    {
        UIManager.getDefaults().put("TitledBorder.titleColor", Color.RED);
        Border lowerEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        TitledBorder title = BorderFactory.createTitledBorder(lowerEtched, "Title");
//      title.setTitleJustification(TitledBorder.RIGHT);
        Font titleFont = UIManager.getFont("TitledBorder.font");
        title.setTitleFont( titleFont.deriveFont(Font.ITALIC + Font.BOLD) );

        JPanel panel = new JPanel()
        {
            @Override
            public String getToolTipText(MouseEvent e)
            {
                Border border = getBorder();

                if (border instanceof TitledBorder)
                {
                    TitledBorder tb = (TitledBorder)border;
                    FontMetrics fm = getFontMetrics( tb.getTitleFont() );
                    int titleWidth = fm.stringWidth(tb.getTitle()) + 20;
                    Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());
                    return bounds.contains(e.getPoint()) ? super.getToolTipText() : null;
                }

                return super.getToolTipText(e);
            }
        };
        panel.setBorder( title );
        panel.setToolTipText("Title With ToolTip");

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

此代码假定标题位于左侧。如果您想要右侧的标题,则需要调整文本边界的X值。

答案 2 :(得分:1)

我认为您无法将setToolTipText添加到TitledBorder。您可以为JComponent提供工具提示,但TitledBorder不是来自JComponent

您可以尝试使用JPanel代替:

 ToolTipManager.sharedInstance().registerComponent(new JPanel());
 //ToolTipManager.sharedInstance().setDismissDelay(800000);

答案 3 :(得分:1)

  • TollTip不正确实验的组件,弹出窗口或工具提示的所有goog变通方法都基于JWindow /未修饰的JDialog

  • 可能没有必要,UIManager中的密钥是可访问的,但在这种情况下,所有TollTips都具有相同的设置

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.*;

public class ColoredToolTipExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public ColoredToolTipExample() {
        Border line, raisedbevel, loweredbevel, title, empty;
        line = BorderFactory.createLineBorder(Color.black);
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        title = BorderFactory.createTitledBorder("");
        empty = BorderFactory.createEmptyBorder(1, 1, 1, 1);
        Border compound;
        compound = BorderFactory.createCompoundBorder(empty, line);
        UIManager.put("ToolTip.foreground", new ColorUIResource(Color.red));
        UIManager.put("ToolTip.background", new ColorUIResource(Color.yellow));
        UIManager.put("ToolTip.font", new FontUIResource(new Font("Verdana", Font.PLAIN, 18)));
        UIManager.put("ToolTip.border", new BorderUIResource(compound));
        JButton button = new JButton("Hello, world");
        button.setToolTipText("<html> - myText <br> - myText <br> - myText <br>");
        getContentPane().add(button);
        JFrame frame = new JFrame("Colored ToolTip Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ColoredToolTipExample();
            }
        });
    }
}