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.
attach a context menu to each point
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: attach a context menu to each point
Depending on context, you may be looking for a ChartMouseListener or a ChartMouseListenerFX.
Re: attach a context menu to each point
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...]
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...]
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: attach a context menu to each point
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());
}
});