-1

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);
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    Is [`JTextField#setText(String)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)) what you're looking for? For better help sooner post a [mcve]. Also take a look at [What is a `NullPointerException`, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – MasterBlaster Feb 20 '17 at 22:32
  • JTextField is not updating the values whenever i click on the points. it is only showing the default value which is set to 1 in my case. i want textfield updates the value on click. – Ankita Jiyani Feb 21 '17 at 05:34

1 Answers1

1

Don't update your text fields unless ce instanceof XYItemEntity is true, like they show here.

chartPanel.addChartMouseListener(new ChartMouseListener() {
    public void chartMouseClicked(final ChartMouseEvent event) {
        ChartEntity ce = cme.getEntity();
        XYItemEntity ce = (XYItemEntity) event.getEntity();
        if (ce instanceof XYItemEntity) {
            XYItemEntity e = (XYItemEntity) ce;
            XYDataset d = e.getDataset();
            updateTextFields(e, d); // call your update method
        }
    }
 });
Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42