attach a context menu to each point

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
leopris
Posts: 2
Joined: Wed Jun 14, 2017 7:17 pm
antibot: No, of course not.

attach a context menu to each point

Post by leopris » Mon Jun 26, 2017 5:21 pm

Hello - I want to attach a context menu to each point in an XYPlot but cannot find how to do it completely. I make a JPopupMenu with MenuItem's then want to add that menu to a point. The choices of each point's menu will apply only to that point. The ChartPanel doesn't seem to have a call to help with this but seems like it should. The context menu on the ChartPanel seems to just apply to the whole chart. I'm new to JFreechart so it could be that I'm not recognizing some of the terminology used (Series, Dataset, Entity, etc.) although I'm trying to figure it out.

Can someone please show me how to get the points then attach the menu to each point. (doesn't seem to be an "add" or "set" in anything sounding like a group of points.)

Thanks a lot.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: attach a context menu to each point

Post by John Matthews » Mon Jun 26, 2017 10:19 pm

Depending on context, you may be looking for a ChartMouseListener or a ChartMouseListenerFX.

leopris
Posts: 2
Joined: Wed Jun 14, 2017 7:17 pm
antibot: No, of course not.

Re: attach a context menu to each point

Post by leopris » Tue Jun 27, 2017 12:27 am

Thanks, yes, a ChartMouseListener on each point seems to be exactly right. Then in mouseClicked, detect the right-mouse-button, and make the popup menu visible.

I can get the (x,y) of the points now but it is not possible to add a popup menu to a Number or double, for example. I'm looking for how to attach the JPopupMenu to the graphical thingy representing a point on the chart - the dot on the chart showing where the (x,y) point is. I'm not sure if the graphical thingy is an image like a png or jpg -or- if it is a drawn thing like with drawRectangle() -or- what-have-you. In any case, the menu has to be attached (java does an add on a JPanel, for example) to a widget before any listener is going to be effective. How to do that is what I'm looking for.

This was supposed to be a minor enhancement of an older program. Ha!
<Sure, she says, I can do it. Easy-peasy to add a popup menu on a panel full of widgets - all in Java - no sweat. But wait - it isn't just Java; its something else called JFreeChart sitting on top of Java. What??!!! What is *that*? - Sigh.> [Well, neat program, JFreeChart, but seems to be keeping me from doing what I want/need to do...]

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: attach a context menu to each point

Post by John Matthews » Tue Jun 27, 2017 11:37 am

Start with a minimal listener and hover over chart elements to find the entity you want; search for related examples.

Code: Select all

chartPanel.addChartMouseListener(new ChartMouseListener() {

    @Override
    public void chartMouseClicked(ChartMouseEvent e) {
        // display popup here
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent e) {
        System.out.println(e.getEntity());
    }
});

Locked