简体   繁体   English

根据内容设置JTable的行不可编辑

[英]Set row of JTable non-editable depending on contents

I cannot find a solution for setting the cells of a row to non-editable depending on their contents. 我找不到根据其内容将行的单元格设置为不可编辑的解决方案。

There is a combobox in the first column of the JTable (called "CA") and a user can add more rows and choose the values they want. JTable的第一列(称为“ CA”)中有一个组合框,用户可以添加更多行并选择所需的值。 A new row can also be added to the "CA" JTable depending on changes in another JTable . 也可以根据另一个JTable更改将新行添加到“ CA” JTable中。 When the new row is added it is given a specific value in its first column that does not exist in the combo box. 当添加新行时,在组合框中不存在的第一列中将提供一个特定值。

What I want is to make the whole row non-editable when this value appears in its first column. 我想要的是当此值出现在其第一列时使整个行不可编辑。

I am familiar with the isCellEditable method, but I am not sure if it can be used for my "CA" JTable's model to determine if a cell will be editable or not depending on the 1st cow's value. 我熟悉isCellEditable方法,但是不确定是否可以将其用于我的“ CA” JTable's模型来确定单元格是否可编辑,具体取决于第一头母牛的值。 I have also previously used the prepareRenderer method to set the background of a row depending on the values of the cells inside the JTable . 我以前还使用了prepareRenderer方法来设置行的背景,具体取决于JTable内单元格的值。

Is there any way of combining these 2 methods? 有什么方法可以结合这两种方法? If yes, how? 如果是,怎么办? If not, is there any other way? 如果没有,还有其他办法吗? I would appreciate any suggestions. 我将不胜感激任何建议。

This is where I create the "CA" JTable: 这是我创建“ CA” JTable的地方:

//create the List for the ComboBox
Strings createStrings = new Strings();
BillingAccountsCodes = new ArrayList<String>();
BillingAccountsCodes = createStrings.getBillingAccountsCodes();
BillingAccountsCodes.add(0, "Billing Accounts");

//create the combo box
BillingAccountsComboBox = new JComboBox();
for (int i=0; i<BillingAccountsCodes.size(); i++) {
    BillingAccountsComboBox.addItem(BillingAccountsCodes.get(i));
}
AutoCompleteDecorator.decorate(BillingAccountsComboBox);

//create the table's model
String[] columnTitles = {"Billing Account","Document","Billing Service","Notes","Quantity","Value","VAT %","Total"};
modelTableCA = new DefaultTableModel(null,columnTitles);

//set the model for the table and make the columns editable
tableCA = new JTable(modelTableCA){
    public boolean isCellEditable(int row, int column){
        if (column == 7) { //I need this column to always be non-editable
            return false;
        } else {
            return true;
        }
    }
};

tableCA.setSurrendersFocusOnKeystroke(true);

//set the comboboxes to the columns
tableCA.getColumnModel().getColumn(0).setCellEditor(new ComboBoxCellEditor(BillingAccountsComboBox)); //billing acounts column

Solved it like this: 像这样解决它:

tableCA = new JTable(modelTableCA){
    public boolean isCellEditable(int row, int column){
        if (tableCA.getValueAt(row, 0).equals("214") || tableCA.getValueAt(row, 0).equals("A00") || tableCA.getValueAt(row, 0).equals("A30") || tableCA.getValueAt(row, 0).equals("B00")) {
            if (column == 0 || column == 4 || column == 5 || column == 6 || column == 7) {
                return false;
            } else {
                return true;
            }
        } else {
            if (column == 7) {
                return false;
            } else {
                return true;
            }
        }
    }
};

Solved it like this: 像这样解决它:

tableCA = new JTable(modelTableCA){
    public boolean isCellEditable(int row, int column){
        if (tableCA.getValueAt(row, 0).equals("214") || tableCA.getValueAt(row, 0).equals("A00") || tableCA.getValueAt(row, 0).equals("A30") || tableCA.getValueAt(row, 0).equals("B00")) {
            if (column == 0 || column == 4 || column == 5 || column == 6 || column == 7) {
                return false;
            } else {
                return true;
            }
        } else {
            if (column == 7) {
                return false;
            } else {
                return true;
            }
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM