2011-03-22 71 views
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:表格数据在动作执行方法内动态添加。

+1

你应该张贴满工人阶级..它不是某些部分.. – 2011-03-22 09:32:34

回答

1

这似乎是工作(我不得不编造实验数据,你没有提供一个可运行的例子)

import java.awt.BorderLayout 
import javax.swing.DefaultCellEditor 
import javax.swing.JComboBox 
import javax.swing.JFrame 
import javax.swing.JTable 
import javax.swing.table.AbstractTableModel 

// Define the model class with hardcoded data 
class MyModel extends AbstractTableModel { 
    Object[][] data = [ [ 'a', 'one' ], [ 'b', 'two' ], [ 'c', 'three' ] ] 
    String[] columnNames = [ 'Key', 'Value' ] 

    int  getColumnCount()     { columnNames?.length ?: 0 } 
    int  getRowCount()      { data?.length ?: 0 } 
    String getColumnName(int col)   { columnNames[ col ] } 
    Object getValueAt(int row, int col)  { data[ row ][ col ] } 
    Class getColumnClass(int col)   { getValueAt(col, 0).class } 
    boolean isCellEditable(int row, int col) { col == 1 } 

    void setValueAt(Object value, int row, int col) { 
    data[ row ][ col ] = value 
    fireTableCellUpdated(row, col) 
    } 
} 

// Test the model class 
table = new JTable(new MyModel()) 
combo = new JComboBox([ 'one', 'two', 'three' ] as Object[]) 
table.columnModel.getColumn(1).cellEditor = new DefaultCellEditor(combo) 

// And make a frame to run it all in 
new JFrame('Test').with { 
    layout = new BorderLayout() 
    add table, BorderLayout.CENTER 
    pack() 
    defaultCloseOperation = HIDE_ON_CLOSE 
    visible = true 
}