簡體   English   中英

從JComboBox Renderer獲取工具提示

[英]Get tooltip from JComboBox Renderer

我有一個擴展JPanel的ComboBox渲染器,有兩個標簽。 在這里,我需要在鼠標轉到iconLabel時顯示工具提示。 如果mouseIlabelItem中,則不應顯示工具提示。

    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;
      }
  }

當鼠標懸停在圖標區域時,我的工具提示才會出現。這意味着用戶只需要獲得他們想要的工具提示。請幫助。

覆蓋MyItemRenderergetToolTipText(MouseEvent event)方法,將MouseEvent轉換為iconLabel的坐標空間(或者您想要的任何組件)。 如果MouseEvent落在組件的邊界內,則返回不同的結果

就像是...

    @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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM