I am trying to change the text of JTextField
while clicking on the JFreeChart
.
I have defined two classes, defined the JTextField
in one class named as First
and JFreeChart
in other class named as Second
. And I have created a code for retrieving the value of x and y co-ordinates when I click on the points drawn on the chart.
The code is using chart mouse listener for retrieving the co-ordinates. Now I want to pass these coordinates values into the text field defined in other class First
. But I am unable to get these values dynamically.
I have created four text field, but I want only two of them to be dynamically update the value on the click. The text field is showing the value 1 for newX
and newY
which I have assigned. If I remove the value 1 which I have assigned to the variable newX
and newY
, it is showing NullPointerException
.
Suggest me a good way to do the same. I want whenever I click on the points drawn on the chart, they are shown in the text field. The text field is showing the value 1 for newX and newY which i have assigned.
The code I have used for retrieving the value of x and y coordinates is as follows and named this class as First:
Number newX = 1; //value assigned
Number newY = 1;
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(final ChartMouseEvent event){
try {
XYItemEntity ce = (XYItemEntity) event.getEntity();
newX = ce.getDataset().getX(ce.getSeriesIndex(),
ce.getItem());
newY = ce.getDataset().getY(ce.getSeriesIndex(), ce.getItem());
// retrieving the x and y co-ordinates.
System.out.println("chartMouseClicked at" + " " + newX + " " + newY);
}
catch (Exception e) {}
}
The code I have used to set the values into TextField
is as follows:
I have created the object of class First and call the variable newX
and newY
in this class.
First f = new First("");
Number x = f.newX; //20;
Number y = f.newY; //30;
int xx = x.intValue();
int yy = y.intValue();
int width = 300;
int height = 350;
// converting value into string for textfield
String x1 = String.valueOf(xx);
String y1 = String.valueOf(yy);
String w1 = String.valueOf(width);
String h1 = String.valueOf(height);
Rectangle r = new Rectangle(xx,yy,width,height);
jf1.setText(x1);
jf2.setText(y1);
jf3.setText(w1);
jf4.setText(h1);