![](/img/trans.png)
[英]Java eclipse : How to add browse button for Table editable cell?
[英]How to change a table cell from editable to not editable with checkbox
我已經在UI上創建了jTable,我想根據其他單元的布爾狀態(復選框)將單元屬性從可編輯更改為不可編輯。 我一直在瀏覽幾個示例,但未能按預期進行操作,主要是因為我犯了一個錯誤,即使用NetBeans創建UI從而創建了很多我什至無法編輯的代碼。
我的表格: 表格http://freepicupload.com/images/337jtable1.png !
編輯:修復,工作,下面的代碼。
生成表/模型的代碼:
jTable1.setModel(new MyTableModel());
表模型及其實現的邏輯:
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Job Type",
"Name",
"avg Time",
"Buffer",
"Buffer Parts",
"Color"};
private Object[][] data = {
{"1", "Station 1",
new Integer(10), new Boolean(false), new Integer(0), Color.red},
{"2", "Station 2",
new Integer(10), new Boolean(false), new Integer(0), Color.blue},
{"3", "Station 3",
new Integer(10), new Boolean(false), new Integer(0), Color.green},
{"4", "Station 4",
new Integer(10), new Boolean(false), new Integer(0), Color.orange},
{"5", "Station 5",
new Integer(10), new Boolean(false), new Integer(0), Color.black}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
@Override
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col == 0) { return false; }
else if (col == 4) {
/*if (getValueAt(row,(col-1)) == "false") { System.out.println("NAO PODES EDITAR BOI"); }
else if (getValueAt(row,(col-1)) == "true") { System.out.println("Podes que eu deixo!"); } */
boolean di = (Boolean) getValueAt(row,(col-1));
if (!di) { return false; }
else { return true; }
}
else { return true; }
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
在這種情況下,只有選中第3列的復選框(狀態為true),才允許編輯第4列的單元格。 希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.