如何通过列名获取/设置JTable的单元格值

Har*_*sha 0 java swing jtable

是否可以按列名获取或设置JTable的单元格值?

Sey*_*mad 6

没有在JTable中找到内置方法,但是这个怎么样:

private int getColumnByName(JTable table, String name) {
    for (int i = 0; i < table.getColumnCount(); ++i)
        if (table.getColumnName(i).equals(name))
            return i;
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下设置和获取单元格值:

table.setValueAt(value, rowIndex, getColumnByName(table, colName));

table.getValueAt(rowIndex, getColumnByName(table, colName));
Run Code Online (Sandbox Code Playgroud)