在 JTable 中自动从特定列获取所有 ID(即不必 select 行)

Getting all the ID's from a specific column automatically (i.e. not having to select a row) in a JTable

正如标题所暗示的那样,我希望能够从 JTable 中获取 ID 列中的所有值,而无需用户实际选择行(因此选择 ID)。假设有两个结果JTable,这意味着 ID 列中有两个项目。有没有办法自动提取这些值。

很大程度上取决于您的目标,但您可以

  • 遍历TableModel,为每一列调用getColumnClass();使用 getValueAt() 检查特定行的值。

    DefaultTableModel model = new DefaultTableModel();
    …
    for (int col = 0; col < model.getColumnCount(); col++) {
        Class type = model.getColumnClass(col);
    }
    
  • 使用 DatabaseMetaData & ResultSetMetaData 的方法获取关系属性的详细信息, 将数据插入 TableModel 之前。