DISPLAYING ELEMENT-LEVEL TOOL TIPS FOR SWING COMPONENTS

Swing组件元素级工具提示的显示方法
部署运行你感兴趣的模型镜像

DISPLAYING ELEMENT-LEVEL TOOL TIPS FOR SWING COMPONENTS

Tool tips are those little popup windows that appear when you rest your mouse pointer over a control such as a button or list. All Swing components support tool tips. To establish a tool tip for a control, call setToolTipText() for the component. When a user rests the mouse pointer over the control, the tool tip automatically appears. Typically, you want one text string for an entire component. But there can be times when you want the text to be more flexible. For instance, in the case of a JList, JTree, or JTable component, you might want to display tool tip text based on which list entry, tree node, or table cell the mouse pointer is over. How can you customize the text?

There are two ways you can create custom tool tips for these Swing components. You can customize the control and override the public String getToolTipText(MouseEvent event) method, or you can customize the renderer for the control and let the renderer deal with the tool tips.

If you decide to override the getToolTipText() method, you have to understand that the passed-in MouseEvent contains the mouse coordinates over the control. It is your responsibility to determine what element the cursor is on. Then, you have to look up the appropriate tool tip text for the element and return that text. You must then manually register the component with the ToolTipManager. If you called the setToolTipText() method for the control, a call to the registerComponent() method of ToolTipManager would have been done for you, but because you don't need to call setToolTipText(), you must do the registration yourself.

Here's an example that demonstrates this first approach. The example uses the keys for the system properties (such as java.runtime.name) as the list entries. The example uses the property values (such as Java 2, Runtime Environment, Standard Edition) as the tool tip text for those entries.

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

    public class ListTest extends JList {

      DefaultListModel model;
      Properties tipProps;

      public ListTest(Properties props) {
        model = new DefaultListModel();
        setModel(model);
        ToolTipManager.sharedInstance().registerComponent(
                                             this);

        tipProps = props;
        Enumeration enum = props.propertyNames();
        while (enum.hasMoreElements()) {
          model.addElement(enum.nextElement());
        }
      }
      public String getToolTipText(MouseEvent event) {
        Point p = event.getPoint();
        int location = locationToIndex(p);
        String key = (String)model.getElementAt(
                                            location);
        String tip = tipProps.getProperty(key);
        return tip;
      }
      public static void main (String args[]) {
        JFrame frame = new JFrame("Custom Tips");
        frame.setDefaultCloseOperation(
                                JFrame.EXIT_ON_CLOSE);
        Properties props = System.getProperties();
        ListTest list = new ListTest(props);
        JScrollPane scrollPane = new JScrollPane(list);
        frame.getContentPane().add(scrollPane);
        frame.setSize(300, 300);
        frame.show();
      }
    }

If you decide instead to customize the cell renderer, you have to do a little more work at first, but you don't have to determine what element is under the mouse at run time. If you find that the tool tips are hardly used, though, this extra work might not be necessary. That's because getting cell renderers happens frequently, and the tool tip setup happens every time the cell is rendered. It might be better to use the first mechanism. Essentially, what it involves is calling the setToolTipText() method for the cell renderer.

Here's an example that demonstrates the cell renderer approach. The example is taken from the book "John Zukowski's Definitive Guide to Swing for Java 2, Second Edition" published by Apress.

First the renderer: this code essentially reuses a DefaultTreeCellRenderer as the renderer, setting its tip text.

    import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.*;
    import java.util.*;

    public class ToolTipTreeCellRenderer 
        implements TreeCellRenderer {
      DefaultTreeCellRenderer renderer = 
        new DefaultTreeCellRenderer();
      Dictionary tipTable;

      public ToolTipTreeCellRenderer (
                             Dictionary tipTable) {
        this.tipTable = tipTable;
      }

      public Component getTreeCellRendererComponent(
          JTree tree, Object value, boolean selected, 
          boolean expanded, boolean leaf, int row, 
          boolean hasFocus) {
        renderer.getTreeCellRendererComponent(
          tree, value, selected, expanded, leaf, row, 
                                            hasFocus);
        if (value != null) {
          Object tipKey;
          if (
             value instanceof DefaultMutableTreeNode) {
             tipKey = 
              ((DefaultMutableTreeNode)value).getUserObject();
          } else {
            tipKey = tree.convertValueToText(value, 
              selected, expanded, leaf, row, hasFocus);
          }
          Object tip = tipTable.get(tipKey);
          if (tip != null) {
            renderer.setToolTipText(tip.toString());
          } else {
            renderer.setToolTipText(null);
          }
        }
        return renderer;
      }
    }

The test program then just registers the tree with the ToolTipManager and registers the renderer. There is no need to subclass JTree. Like the JList example, the system properties are used for the component elements. In this case, the tree is only one level deep.

    import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.*;
    import java.util.*;

    public class TreeTips {
      public static void main(String args[]) {
        JFrame frame = new JFrame("Custom Tree Tips");
        frame.setDefaultCloseOperation(
                                 JFrame.EXIT_ON_CLOSE);
        Properties props = System.getProperties();
        JTree tree = new JTree(props);
        ToolTipManager.sharedInstance().registerComponent(
                                                 tree);
        TreeCellRenderer renderer = 
                    new ToolTipTreeCellRenderer(props);
        tree.setCellRenderer(renderer);
        JScrollPane scrollPane = new JScrollPane(tree);
        frame.getContentPane().add(
                      scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    }
<!-- Black Horizontal Line Begins Here -->

您可能感兴趣的与本文相关的镜像

Qwen-Image-Edit-2509

Qwen-Image-Edit-2509

图片编辑
Qwen

Qwen-Image-Edit-2509 是阿里巴巴通义千问团队于2025年9月发布的最新图像编辑AI模型,主要支持多图编辑,包括“人物+人物”、“人物+商品”等组合玩法

### PrimeFaces 6.2 Tooltip 白色背景设置方法 在 PrimeFaces 中,可以通过自定义 CSS 样式来调整 `p:tooltip` 的背景颜色。具体来说,可以使用 `styleClass` 属性为 tooltip 指定一个自定义样式类,并通过外部 CSS 文件或 `<style>` 标签定义该类的样式[^1]。 以下是一个示例代码: ```xml <p:tooltip for="button" value="This is a tooltip" styleClass="custom-tooltip" /> <p:commandButton id="button" value="Hover Me" /> ``` 在 CSS 文件中定义白色背景的样式: ```css .custom-tooltip .ui-tooltip-content { background-color: white !important; /* 设置背景颜色为白色 */ color: black; /* 确保文字颜色与背景形成对比 */ border: 1px solid #ccc; /* 可选:添加边框以增强视觉效果 */ } ``` ### showEffect 参数中的 fade 效果含义和用法 `showEffect` 是 PrimeFaces 中用于控制组件显示动画效果的属性。当 `showEffect` 设置为 `fade` 时,表示组件在显示时会应用淡入(fade-in)的动画效果[^2]。 以下是使用 `showEffect` 和 `fade` 动画效果的示例代码: ```xml <p:tooltip for="button" value="This is a tooltip" showEffect="fade" hideEffect="fade" /> <p:commandButton id="button" value="Hover Me" /> ``` 在此示例中: - `showEffect="fade"` 表示当 tooltip 显示时会应用淡入效果。 - `hideEffect="fade"` 表示当 tooltip 隐藏时会应用淡出效果。 如果需要自定义动画的持续时间,可以使用 `showEffectDuration` 和 `hideEffectDuration` 属性。例如: ```xml <p:tooltip for="button" value="This is a tooltip" showEffect="fade" showEffectDuration="500" hideEffect="fade" hideEffectDuration="500" /> ``` 上述代码将淡入和淡出动画的持续时间设置为 500 毫秒。 ### 注意事项 - 在定义 `styleClass` 时,确保覆盖 PrimeFaces 默认的样式规则,因此通常需要使用 `!important` 来提高样式的优先级。 - `showEffect` 和 `hideEffect` 支持多种动画效果,包括但不限于 `fade`、`blind`、`clip`、`drop` 等[^2]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值