0
我想添加一个组合框作为单元格编辑器到我的JTable。为了实现这一点,我使用了如下的column.setCellEditor方法。但是我发现它不起作用。我怎样才能解决这个问题?setcelleditor combobox Jtable不工作
def treeModel = new GrvTableModel()
table = new JTable(treeModel)
TableColumn col = table.getColumnModel().getColumn(1);
JComboBox com = new JComboBox();
com.addItem("one");
com.addItem("two");
com.addItem("three");
col.setCellEditor (new DefaultCellEditor(com));
.......................
class GrvTableModel extends AbstractTableModel {
public String[] columnNames = [ "key" , "value"]
public Object[][] data = []
public selectedObjArray
//Returns the number of columns in the datamodel
public int getColumnCount() {
return columnNames.length;
}
//Returns the number of rows in the datamodel
public int getRowCount() {
return data.length;
}
//Returns the name of column in the datamodel at the specified index
public String getColumnName(int col) {
return columnNames[col];
}
//Returns the object at the specified [row,column] index
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col == 1) {
return true;
} else {
return false;
}
}
//sets the object at the row and column as specified
public void setValueAt(Object value, int row, int col) {
println "change table row: " + row
data[row][col] = value;
fireTableCellUpdated(row, col);
selectedObjArray[row].val = value
}
}
PS:表格数据在动作执行方法内动态添加。
你应该张贴满工人阶级..它不是某些部分.. – 2011-03-22 09:32:34