根据内容

时间:2015-04-22 09:54:10

标签: java swing jtable

我无法找到将行的单元格设置为不可编辑的解决方案,具体取决于其内容。

JTable的第一列(称为“CA”)中有一个组合框,用户可以添加更多行并选择所需的值。根据另一个JTable的更改,还可以向“CA”JTable添加新行。添加新行时,在第一列中给出一个特定值,该值在组合框中不存在。

我想要的是当这个值出现在第一列时,整行不可编辑。

我熟悉isCellEditable方法,但我不确定它是否可用于我的“CA”JTable's模型,以确定单元格是否可编辑,具体取决于第1个牛的价值。我之前也使用prepareRenderer方法根据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

解决这个问题:

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

1 个答案:

答案 0 :(得分:0)

解决这个问题:

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;
            }
        }
    }
};
相关问题