title: Correctly getting data from a (sorted) JTable
body: <p>I have developed a basic custom JTableModel as follows</p>
  
  <pre><code>public class CustomTableModel extends DefaultTableModel {
    List&lt;MyClass&gt; data;
    public CustomTableModel(List&lt;MyClass&gt; data) {
      this.data = data;
    }
  
    public Class&lt;?&gt; getColumnClass(int columnIndex) {
      return MyClass.class;
    }
  
    public MyClass getValueAt(int rowIndex, int columnIndex) {
      return data.get(rowIndex);
    }
  
    // ...
  }
  </code></pre>
  
  <p>I then use a basic custom JTableCellRenderer as follows</p>
  
  <pre><code>public class CustomTableCellRenderer extends JLabel implements TableCellRenderer {
  
    public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
  
      MyClass myClass = (MyClass)value;
  
      lbl.setText(myClass.getString());
  
      return this;
    }
  }
  </code></pre>
  
  <p>I also have a custom JPanel that displays various information as follows</p>
  
  <pre><code>public class MyPanel extends JPanel {
    private MyClass myClass;
  
    public MyPanel(MyClass myClass) {
      // initialize components
    }
  
    public setMyClass(MyClass myClass) {
      this.myClass = myClass;
      updateFields();
    }
  
    private void updateFields() {
      this.fieldString.setText(myClass == null ? "" : myClass.getString());
      // ...
    }
  }
  </code></pre>
  
  <p>Finally, I use a table to list my data and the custom panel to display the details of the selected data.</p>
  
  <pre><code>public class JCustomFrame extends JFrame {
    public JCustomFrame(List&lt;MyClass&gt; data) {
      // ...
      JTable table = new JTable(new CustomTableModel(data));
      table.setDefaultRenderer(MyClass.class, new CustomTableCellRenderer());
  
    }
  }
  </code></pre>
  
  <p>What I am trying to accomplish is get the selected MyClass from the table <strong>regardless of sorting</strong>.</p>
  
  <p>I tried ListSelectionListener but the methods do not return anything other than the selected indexes. Even if I have the index, if the table is sorted, my model is not so sophisticated and will return the wrong object.</p>