[英]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.