考虑到该类实际上并未保存LayoutFactory创建的JPopupMenu,是否可以向JChart2D的ChartPanel弹出菜单添加菜单项?

最佳答案

我找到了解决方案,不确定是否最好:

public class MyChartpanel extends ChartPanel {
  private JPopupMenu popup;
  public MyChartpanel(Chart2D chart) {
    super(chart);
    MouseListener[] listeners = chart.getMouseListeners();

    for (MouseListener listener : listeners) {
      if (listener instanceof PopupListener) {
        PopupListener popupListener = (PopupListener) listener;
        popup = popupListener.getPopup();
        popup.add(new MyMenuItem());
      }
    }
  }
}

08-18 11:19