my mouselistener code is working but on the next subsequents clicks, it shows the previous clicked also.
so how can i update my mouse click?
i used scopeChartComposite.update()
but after compiling it says cant find symbol - method scopeChartComposite
how can i solve it?
Regards,
Anamika
MousecClickedEvent update
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: MousecClickedEvent update
What is that?anamika21 wrote:i used scopeChartComposite.update()
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


this is the code to get X,Y coordinates whenever mouse is clicked on chartPanel..
class MyChartMouseListener implements ChartMouseListener {
MouseEvent tr;
int x, y;
double chartX, chartY;
int ctrlShiftClickMask = InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK;
JFreeChart cp;
XYPlot scopeXYPlot ;
Color markerLineColor=Color.red;
Color markerLabelColor=Color.red;
// int BOLD;
// Font markerLabelFont= Font.int BOLD;
// Shape markerStroke = Shape.Rectangle;
String NL = "\n";
public void chartMouseClicked(ChartMouseEvent e) {
tr = e.getTrigger();
System.out.printf("MyChartMouseListener: ChartMouseEvent %s triggered by %s occurred\n", e.getSource().toString(), tr.paramString());
//Crosshair values are not valid until after the chart has been updated
// scopeChartComposite.update();
scopeXYPlot = cp.getXYPlot();
if (tr.getButton() == MouseEvent.BUTTON1) {
if ((tr.getModifiersEx() & ctrlShiftClickMask) == ctrlShiftClickMask) {
scopeXYPlot.clearDomainMarkers();
scopeXYPlot.clearRangeMarkers();
//Ctrl-Shift Click is used to remove an existing marker
ValueMarker marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
// marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
marker.setPaint(markerLineColor);
marker.setLabel(String.format("X: %-1.3f%sY: %-1.3f", scopeXYPlot.getDomainCrosshairValue(), NL, scopeXYPlot.getRangeCrosshairValue()));
marker.setLabelPaint(markerLabelColor);
marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
// marker.setLabelFont(markerLabelFont);
// marker.setStroke(markerStroke);
// scopeXYPlot.removeDomainMarker(marker);
scopeXYPlot.addDomainMarker(marker);
marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
marker.setPaint(markerLineColor);
// marker.setStroke(markerStroke);
// scopeXYPlot.removeRangeMarker(marker);
scopeXYPlot.addRangeMarker(marker);
System.out.printf("MyChartMouseListener: Ctrl-Shift-Click detected, marker set [%s]\n", marker.getLabel());
// scopeXYPlot.update();
// scopeChartComposite.update();
} else if ((tr.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == MouseEvent.CTRL_DOWN_MASK) {
// Left mouse Ctrl-Click occurred
ValueMarker marker = new ValueMarker(scopeXYPlot.getDomainCrosshairValue());
// marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
marker.setPaint(markerLineColor);
marker.setLabel(String.format("X: %-1.3f%sY: %-1.3f", scopeXYPlot.getDomainCrosshairValue(), NL, scopeXYPlot.getRangeCrosshairValue()));
marker.setLabelPaint(markerLabelColor);
marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
// marker.setLabelFont(markerLabelFont);
// marker.setStroke(markerStroke);
scopeXYPlot.addDomainMarker(marker);
marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
marker.setPaint(markerLineColor);
// marker.setStroke(markerStroke);
scopeXYPlot.addRangeMarker(marker);
System.out.printf("MyChartMouseListener: Ctrl-Click detected, marker added [%s]\n", marker.getLabel());
// scopeChartComposite.update();
} else {
//Normal left mouse click occurred
System.out.printf("MyChartMouseListener: normal click detected; params = [%s] detected\n", tr.paramString());
double crossHairX = scopeXYPlot.getDomainCrosshairValue();
double crossHairY = scopeXYPlot.getRangeCrosshairValue();
// XLabel.setText(String.format("X: (Sec)%s%8.4f", NL, crossHairX));
// YLabel.setText(String.format("Y: (P/U)%s%8.4f", NL, crossHairY));
}
} else {
System.out.printf("MyChartMouseListener: some other button pressed [e=%s]\n", tr.paramString());
}
}
public void chartMouseMoved(ChartMouseEvent e) {
// Nothing to do. (?)
}
MyChartMouseListener(JFreeChart chart)
{
cp = chart;
}
}
when i am clicking on chartpanel..its showing coordinates but on differnet location and second after the subsequent clicks..it shows the previous mouseclicked valued..how can i remove it??
regards,
Anamika
class MyChartMouseListener implements ChartMouseListener {
MouseEvent tr;
int x, y;
double chartX, chartY;
int ctrlShiftClickMask = InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK;
JFreeChart cp;
XYPlot scopeXYPlot ;
Color markerLineColor=Color.red;
Color markerLabelColor=Color.red;
// int BOLD;
// Font markerLabelFont= Font.int BOLD;
// Shape markerStroke = Shape.Rectangle;
String NL = "\n";
public void chartMouseClicked(ChartMouseEvent e) {
tr = e.getTrigger();
System.out.printf("MyChartMouseListener: ChartMouseEvent %s triggered by %s occurred\n", e.getSource().toString(), tr.paramString());
//Crosshair values are not valid until after the chart has been updated
// scopeChartComposite.update();
scopeXYPlot = cp.getXYPlot();
if (tr.getButton() == MouseEvent.BUTTON1) {
if ((tr.getModifiersEx() & ctrlShiftClickMask) == ctrlShiftClickMask) {
scopeXYPlot.clearDomainMarkers();
scopeXYPlot.clearRangeMarkers();
//Ctrl-Shift Click is used to remove an existing marker
ValueMarker marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
// marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
marker.setPaint(markerLineColor);
marker.setLabel(String.format("X: %-1.3f%sY: %-1.3f", scopeXYPlot.getDomainCrosshairValue(), NL, scopeXYPlot.getRangeCrosshairValue()));
marker.setLabelPaint(markerLabelColor);
marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
// marker.setLabelFont(markerLabelFont);
// marker.setStroke(markerStroke);
// scopeXYPlot.removeDomainMarker(marker);
scopeXYPlot.addDomainMarker(marker);
marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
marker.setPaint(markerLineColor);
// marker.setStroke(markerStroke);
// scopeXYPlot.removeRangeMarker(marker);
scopeXYPlot.addRangeMarker(marker);
System.out.printf("MyChartMouseListener: Ctrl-Shift-Click detected, marker set [%s]\n", marker.getLabel());
// scopeXYPlot.update();
// scopeChartComposite.update();
} else if ((tr.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == MouseEvent.CTRL_DOWN_MASK) {
// Left mouse Ctrl-Click occurred
ValueMarker marker = new ValueMarker(scopeXYPlot.getDomainCrosshairValue());
// marker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
marker.setPaint(markerLineColor);
marker.setLabel(String.format("X: %-1.3f%sY: %-1.3f", scopeXYPlot.getDomainCrosshairValue(), NL, scopeXYPlot.getRangeCrosshairValue()));
marker.setLabelPaint(markerLabelColor);
marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
// marker.setLabelFont(markerLabelFont);
// marker.setStroke(markerStroke);
scopeXYPlot.addDomainMarker(marker);
marker = new ValueMarker(scopeXYPlot.getRangeCrosshairValue());
marker.setPaint(markerLineColor);
// marker.setStroke(markerStroke);
scopeXYPlot.addRangeMarker(marker);
System.out.printf("MyChartMouseListener: Ctrl-Click detected, marker added [%s]\n", marker.getLabel());
// scopeChartComposite.update();
} else {
//Normal left mouse click occurred
System.out.printf("MyChartMouseListener: normal click detected; params = [%s] detected\n", tr.paramString());
double crossHairX = scopeXYPlot.getDomainCrosshairValue();
double crossHairY = scopeXYPlot.getRangeCrosshairValue();
// XLabel.setText(String.format("X: (Sec)%s%8.4f", NL, crossHairX));
// YLabel.setText(String.format("Y: (P/U)%s%8.4f", NL, crossHairY));
}
} else {
System.out.printf("MyChartMouseListener: some other button pressed [e=%s]\n", tr.paramString());
}
}
public void chartMouseMoved(ChartMouseEvent e) {
// Nothing to do. (?)
}
MyChartMouseListener(JFreeChart chart)
{
cp = chart;
}
}
when i am clicking on chartpanel..its showing coordinates but on differnet location and second after the subsequent clicks..it shows the previous mouseclicked valued..how can i remove it??
regards,
Anamika
You should override the "MouseListener" interface methods from "ChartPanel" instead of the "ChartMouseListener" interface methods.
If you examine the source code for "ChartPanel" you will see that not all mouse events get forwarded to "ChartMouseListener" methods ... so you are listening in the wrong place.
I successfully overrode these methods to provide custom functionality allowing the user either to click on a chart data point or to do a CTRL-zoom-rectangle sequence (so I could then use either the selected point or rectangle to do something with any chart data points that were "selected"). Search for a "TIP" I posted to this forum a week or so ago for some additional info (I just updated that thread with some info on my final solution so you should see it close to your thread).
If you examine the source code for "ChartPanel" you will see that not all mouse events get forwarded to "ChartMouseListener" methods ... so you are listening in the wrong place.
I successfully overrode these methods to provide custom functionality allowing the user either to click on a chart data point or to do a CTRL-zoom-rectangle sequence (so I could then use either the selected point or rectangle to do something with any chart data points that were "selected"). Search for a "TIP" I posted to this forum a week or so ago for some additional info (I just updated that thread with some info on my final solution so you should see it close to your thread).
Unfortunately, my code is on another network with no internet access so it would be a problem ... or would be time consuming to retype ... my code here. The only other alternative for me is to have a friend scan my code with their OCR software, but their program is not all that accurate ... once again requiring yet more time to edit the code to correct all the scan mistakes.
Anyway, I don't think my code is really necessary....
If you look at the source code for the mouse listener methods in the JFreeChart "ChartPanel" class, this should be sufficient to get you started in the correct direction. This is how I started out with my customizations.
If you wanted not only the mouse listener code, but also my code to actually find the chart data points within the rectangle, this code is more involved. I describe the basic approach in the "TIP" thread I posted to this forum that I mentioned before. See this other thread for some additional details.
Anyway, I don't think my code is really necessary....
If you look at the source code for the mouse listener methods in the JFreeChart "ChartPanel" class, this should be sufficient to get you started in the correct direction. This is how I started out with my customizations.
If you wanted not only the mouse listener code, but also my code to actually find the chart data points within the rectangle, this code is more involved. I describe the basic approach in the "TIP" thread I posted to this forum that I mentioned before. See this other thread for some additional details.