Java JTable getting the data of the selected row

前端 未结 4 1975
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 18:43

Are there any methods that are used to get the data of the selected row? I just want to simply click a specific row with data on it and cli

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-01 19:47

    using from ListSelectionModel:

    ListSelectionModel cellSelectionModel = table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        String selectedData = null;
    
        int[] selectedRow = table.getSelectedRows();
        int[] selectedColumns = table.getSelectedColumns();
    
        for (int i = 0; i < selectedRow.length; i++) {
          for (int j = 0; j < selectedColumns.length; j++) {
            selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
          }
        }
        System.out.println("Selected: " + selectedData);
      }
    
    });
    

    see here.

提交回复
热议问题