JTable getSelectedRow does not return the selected row index

Look to your code

private void jbInit() throws Exception {
    ...
    jScrollPane1.getViewport().add(jTable1, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jScrollPane1, null);
    jTable1 = new JTable (model);
    ...

You add jTable1 to the JScrollPane at first and only then you create jTable1. It’s incorrect way. jTable1 variable doen’t linked now with table you place at the form. I think it’s cause of your problem.

Move creation of the jTable1 before adding in into JScrollPane.

...
jTable1 = new JTable (model);
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
...

Leave a Comment