概要

JListのセルレンダラーとして使用しているJEditorPaneに複数リンクを表示し、マウスクリックイベントを転送してHyperlinkEventが発生するように設定します。

サンプルコード

DefaultListModel<SiteItem> m = new DefaultListModel<>();
m.addElement(new SiteItem("aterai",
  Arrays.asList("https://ateraimemo.com", "https://github.com/aterai")));
m.addElement(new SiteItem("example",
  Arrays.asList("http://www.example.com", "https://www.example.com")));

JList<SiteItem> list = new JList<>(m);
list.setFixedCellHeight(120);
list.setCellRenderer(new SiteListItemRenderer());
list.addMouseListener(new MouseAdapter() {
  @Override public void mouseClicked(MouseEvent e) {
    Point pt = e.getPoint();
    int index = list.locationToIndex(pt);
    if (index >= 0) {
      SiteItem item = list.getModel().getElementAt(index);
      Component c = list.getCellRenderer().getListCellRendererComponent(
          list, item, index, false, false);
      if (c instanceof JEditorPane) {
        Rectangle r = list.getCellBounds(index, index);
        c.setBounds(r);
        MouseEvent me = SwingUtilities.convertMouseEvent(list, e, c);
        me.translatePoint(pt.x - r.x - me.getX(), pt.y - r.y - me.getY());
        c.dispatchEvent(me);
        // pt.translate(-r.x, -r.y);
        // c.dispatchEvent(new MouseEvent(
        //     c, e.getID(), e.getWhen(), e.getModifiers() | e.getModifiersEx(),
        //     pt.x, pt.y, e.getClickCount(), e.isPopupTrigger()));
      }
    }
  }
});
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JListに設定したMouseListenerのクリックイベントをそのListCellRendererとして使用しているJEditorPanedispatchEventで転送し、HyperlinkListenerでリンククリックイベントが取得できるように設定しています。

  • MouseEventは、そのクリック位置をJListからJEditorPane相対に変更する必要がある
    • SwingUtilities.convertMouseEvent(list, e, editor)ではうまく変換できないので、自前で位置を変換し、new MouseEvent(...)でイベントを作り直している
    • SwingUtilities.convertMouseEvent(list, e, editor)でマウスイベントのソースをセルエディタに変換し、座標はMouseEvent#translatePoint(...)メソッドを使用してセルエディタ相対に変換
  • MouseEventはそのクリック位置をJListからJEditorPane相対に変更する必要がある
  • SwingUtilities.convertMouseEvent(list, e, editor)でマウスイベントのソースをセルエディタに変換し、座標はMouseEvent#translatePoint(...)メソッドを使用してセルエディタ相対に変換

参考リンク

コメント