settableleditor combobox for Jtable无法正常工作

时间:2011-03-22 09:08:23

标签: java groovy jtable

我想在我的JTable中添加一个组合框作为单元格编辑器。为此,我使用了column.setCellEditor方法,如下所示。但我发现它无法正常工作。我该如何解决这个问题?

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 个答案:

答案 0 :(得分: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
}