JTable getSelectedRow does not return the selected row index

前端 未结 2 2058
孤城傲影
孤城傲影 2020-11-27 08:43

I try to get data in the row that is selected but getSelectedRow() doesn\'t work. Actually, I used that method in the other class, but it works in there. When I

2条回答
  •  天命终不由人
    2020-11-27 09:16

    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);
    ...
    

提交回复
热议问题