[英]Set row of JTable non-editable depending on contents
我找不到根据其内容将行的单元格设置为不可编辑的解决方案。
JTable
的第一列(称为“ CA”)中有一个组合框,用户可以添加更多行并选择所需的值。 也可以根据另一个JTable
更改将新行添加到“ CA” JTable
中。 当添加新行时,在组合框中不存在的第一列中将提供一个特定值。
我想要的是当此值出现在其第一列时使整个行不可编辑。
我熟悉isCellEditable
方法,但是不确定是否可以将其用于我的“ CA” JTable's
模型来确定单元格是否可编辑,具体取决于第一头母牛的值。 我以前还使用了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;
}
}
}
};
像这样解决它:
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.