Click a bar and select the corresponding JTabel record
<p>I have a JScrollPane divided in two <code>Jpanel</code>s as you can see in the picture:</p>

<p><a href="https://i.stack.imgur.com/PnVtj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PnVtj.png" alt="enter image description here"></a></p>

<p>On the right there is a <code>JTable</code> with three columns. On the left there is the corresponding histogram created with <code>JFreeChart</code> API. <br>
I have to implement a listener on the histogram to ensure that when one presses on a bar, the corresponding record in the <code>JTable</code> on the left is selected. </p>

<p>To reach my target I implement this listener:</p>

<pre><code>    chartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent arg0) {
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent arg0) {

            try{
            TableModel model = table.getModel();    
            CategoryItemEntity entity = (CategoryItemEntity) arg0.getEntity();
            Comparable row = entity.getRowKey();
            Comparable col = entity.getColumnKey();
            System.out.println(String.valueOf(row));
            String field = String.valueOf(col);

                for(int i = 0; i&lt;model.getRowCount(); i++) {
                    if(model.getValueAt(i, 0).equals(field)) {
                        table.changeSelection(i, 0, false, false);

                    }
                }
            }
            catch (Exception e) {
                System.out.println("No bars selected");
            }


        }
    });
</code></pre>

<p>This listener works correctly until one orders the JTable by clicking on the header. For example, if one orders the JTable according to <code>Number of occurrences</code>, when one presses a bar of the histogram, the selected row in the <code>JTable</code> does not correspond to the clicked bar.</p>

<p>---Edit</p>

<p>When one presses a bar, I have to selected the correspondig record in the JTable. For example, suppose one presses "Bronx" bar:</p>

<p><a href="https://i.stack.imgur.com/ObNck.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ObNck.png" alt="enter image description here"></a></p>

<p>My code works correctly until one orders the JTable by clicking on the header field (borough, type, Number of Occurrences). </p>