将所有JTable单元设置为不可选 [英] Set all JTable cells unselectable

查看:121
本文介绍了将所有JTable单元设置为不可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个只显示数据的JTable,不允许任何编辑或选择。我通过运行来设置所有单元格是不可编辑的:

I'm trying to create a JTable that simply displays data and does not allow any edits or selections. I set all cells to be uneditable by running:

TableModel model = new DefaultTableModel(data, titles) {
    public boolean isCellEditable(int rowIndex, int mColIndex) {
        return false;
    }
};

但我现在正试图让所有细胞都无法选择。我找到了 setRowSelectionAllowed 方法,该方法允许我在选择单元格时禁用所选的整行,但这并不能阻止单元格被选中。我查看了 DefaultTableModel 的方法,但我没有看到任何 isCellSelectable 方法。有什么建议吗?

But I'm now trying to make all of the cells unselectable as well. I found the setRowSelectionAllowed method which allowed me to disable the whole row being selected when a cell is selected, but that didn't stop the cell from being selectable. I looked through the methods of DefaultTableModel but I didn't seen any isCellSelectable method. Any suggestions?

推荐答案

除了从<$ c返回 false $ c> isCellEditable(),添加这些调用。

In addition to returning false from isCellEditable(), add these invocations.

table.setFocusable(false);
table.setRowSelectionAllowed(false);

这篇关于将所有JTable单元设置为不可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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