从JComboBox Renderer获取工具提示 [英] Get tooltip from JComboBox Renderer

查看:50
本文介绍了从JComboBox Renderer获取工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox渲染器,该渲染器扩展了JPanel并具有两个标签. 在这里,我只需要在鼠标转到 iconLabel 时显示工具提示.如果鼠标位于 labelItem 工具提示中,则不会显示.

I have a ComboBox renderer that extend JPanel and has two labels. In here i need to show tool-tip when mouse goes to iconLabel only. If mouse is in labelItem tool-tip should not show.

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;



    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    import javax.swing.plaf.metal.MetalIconFactory;

    public class MyItemRenderer extends JPanel implements ListCellRenderer
    {
      private JLabel labelItem = new JLabel();
      private JLabel iconLabel = new JLabel();

      public MyItemRenderer() {
          setLayout(new GridBagLayout());
          GridBagConstraints constraints = new GridBagConstraints();
          constraints.fill = GridBagConstraints.HORIZONTAL;
          constraints.weightx = 1.0;
          constraints.insets = new Insets(2, 2, 2, 0);

          labelItem.setOpaque(true);
          labelItem.setHorizontalAlignment(JLabel.LEFT);

          iconLabel.setOpaque(true);
          iconLabel.setHorizontalAlignment(JLabel.RIGHT);
          iconLabel.setPreferredSize(new Dimension(14, 16));
          iconLabel.setMaximumSize(new Dimension(14, 16));


          add(labelItem, constraints);

          GridBagConstraints constraints1 = new GridBagConstraints();
          constraints1.weightx = 0;
          constraints1.insets = new Insets(0, 0, 0, 2);

          add(iconLabel, constraints1);

          setBackground(Color.LIGHT_GRAY);

      }

      @Override
      public Component getListCellRendererComponent(JList list, Object value,
              int index, boolean isSelected, boolean cellHasFocus) {
          String item = (String) value;

          labelItem.setText(item);

          // set icon
          iconLabel.setIcon(MetalIconFactory.getFileChooserDetailViewIcon());

          if (isSelected) {
              labelItem.setBackground(Color.BLUE);
              labelItem.setForeground(Color.YELLOW);
              iconLabel.setBackground(Color.BLUE);
              iconLabel.setForeground(Color.YELLOW);
          } else {
              labelItem.setForeground(Color.BLACK);
              labelItem.setBackground(Color.LIGHT_GRAY);
              iconLabel.setForeground(Color.BLACK);
              iconLabel.setBackground(Color.LIGHT_GRAY);
          }

          return this;
      }
  }

我的工具提示仅在将鼠标移到图标区域时才需要出现.这意味着用户只需要他们想要的工具提示即可.

My tool-tip only need come when mouse over the icon area.That mean user need to get tool-tip only they want.Please help.

推荐答案

覆盖MyItemRenderergetToolTipText(MouseEvent event)方法,将MouseEvent转换为iconLabel的坐标空间(或所需的任何分量) ).如果MouseEvent处于组件的范围内,则返回不同的结果

Override the getToolTipText(MouseEvent event) method of the MyItemRenderer, translate the MouseEvent into the coordinate space of the iconLabel (or what ever component you want). If the MouseEvent falls within the bounds of the component, then return a different result

类似...

    @Override
    public String getToolTipText(MouseEvent event) {
        String tooltip = super.getToolTipText(event);
        Point p = SwingUtilities.convertPoint(this, event.getPoint(), iconLabel);
        if (iconLabel.contains(p)) {
            tooltip = "I'm a banana";
        }
        return tooltip;
    }

这篇关于从JComboBox Renderer获取工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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