如何通过其列名获取/设置JTable的单元格值 [英] How to get/set the cell value of a JTable by its column name

查看:334
本文介绍了如何通过其列名获取/设置JTable的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to get or set the the cell value of a JTable by column name?

推荐答案

在JTable中找不到内置方法,但是如何处理:

Didn't find a built in method in JTable, but how about this:

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;
}

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

Then you can use the following to set & get cell values :

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

table.getValueAt(rowIndex, getColumnByName(table, colName));

这篇关于如何通过其列名获取/设置JTable的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆