概要

JToolTipを半透明に設定し、その形状や表示位置も変更します。

サンプルコード

class BalloonToolTip extends JToolTip {
  private static final int TRI_HEIGHT = 4;
  private HierarchyListener listener;

  @Override public void updateUI() {
    removeHierarchyListener(listener);
    super.updateUI();
    listener = e -> {
      Component c = e.getComponent();
      if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0
          && c.isShowing()) {
        Optional.ofNullable(SwingUtilities.getRoot(c))
           .filter(JWindow.class::isInstance).map(JWindow.class::cast)
           .ifPresent(w -> w.setBackground(new Color(0x0, true)));
      }
    };
    addHierarchyListener(listener);
    setOpaque(false);
    setForeground(Color.WHITE);
    setBackground(new Color(0xC8_00_00_00, true));
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5 + TRI_HEIGHT, 5));
  }

  @Override public Dimension getPreferredSize() {
    Dimension d = super.getPreferredSize();
    d.height = 32;
    return d;
  }

  @Override protected void paintComponent(Graphics g) {
    Shape s = makeBalloonShape();
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(getBackground());
    g2.fill(s);
    g2.dispose();
    super.paintComponent(g);
  }

  private Shape makeBalloonShape() {
    int w = getWidth() - 1;
    int h = getHeight() - TRI_HEIGHT - 1;
    int r = 10;
    int cx = getWidth() / 2;
    Polygon triangle = new Polygon();
    triangle.addPoint(cx - TRI_HEIGHT, h);
    triangle.addPoint(cx, h + TRI_HEIGHT);
    triangle.addPoint(cx + TRI_HEIGHT, h);
    Area area = new Area(new RoundRectangle2D.Float(0, 0, w, h, r, r));
    area.add(new Area(triangle));
    return area;
  }
}
View in GitHub: Java, Kotlin

解説

  • 半透明化と形状の変更
    • JToolTip自体はsetOpaque(false)で透明化し、別途JToolTip#paintComponent(...)をオーバーライドして吹き出し図形を半透明色で描画
  • 表示位置の変更
    • JList#getToolTipLocation(...)をオーバーライドして、対象セルの真上にJToolTipが表示されるよう位置を調整
      @Override public Point getToolTipLocation(MouseEvent e) {
        Point p = e.getPoint();
        ListCellRenderer<? super Contribution> r = getCellRenderer();
        int i = locationToIndex(p);
        Rectangle rect = getCellBounds(i, i);
        if (i >= 0 && Objects.nonNull(r) && Objects.nonNull(rect) && rect.contains(p.x, p.y)) {
          Contribution value = getModel().getElementAt(i);
          Component renderer = r.getListCellRendererComponent(this, value, i, false, false);
          if (renderer instanceof JComponent) {
            String tipText = ((JComponent) renderer).getToolTipText();
            JToolTip tip = createToolTip();
            tip.setTipText(tipText);
            Dimension d = tip.getPreferredSize();
            int gap = 2;
            return new Point((int) (rect.getCenterX() - d.getWidth() / 2d), rect.y - d.height - gap);
          }
        }
        return null;
      }
      

参考リンク

コメント