Search code examples
javaswingchartsjpopupmenu

Adding popup menu items to JChart2D's ChartPanel


Is it possible to add menu items to JChart2D's ChartPanel pop up menu, considering the class doesn't actually save the JPopupMenu created by the LayoutFactory?


Solution

  • I found a solution, not sure if it's the best:

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