想要其中一个为combobox,其他为textfield
1,我发现用jtable 的 prepareEditor(), 没有效果, ???2,于是用方法二继承DefaultCellEditor,覆写getTableCellEditorComponent(), 结果取不到值???附上部分代码:
public class TestTableCellEditor extends DefaultCellEditor {
    JComboBox comboBox = new JComboBox(new String[] {"a", "b"});    public TestTableCellEditor () {
        super(new JTextField());
    }    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {
        if (row == 0 && column == 0) {
            comboBox.setSelectedItem(value);
            return comboBox;
        } else {
            return super.getComponent();
        }
    }
}

解决方案 »

  1.   

    tb.getColumnModel().getColumn(col).setCellEditor(new DefaultCellEditor(jComboBox));
      

  2.   

    我不是要设一列的combobox
    我一列中有一个combobox, 其他为textfield
      

  3.   

    表中加入数据时,对应的那个单元格加入组件JComboBox 即可。
      

  4.   

    不行的
     那样的话 
     它会把 JComboBox 自动转化成字符串显示      很长的一串~~~~
      

  5.   

    找到解决方法了,有两个方法实现
    方法一,仿照DefaultCellEditor写个editor
    方法二,简单点,要的话留邮箱吧,懒得贴代码了
      

  6.   

    [email protected]
      谢谢!!!
      

  7.   

    不要去继承这个DefaultCellEditor ,自己写一个类extends AbstractCellEditor implements
    TableCellEditor 
    因我写的代码太长,而且还关系到其他的一些东西,就不在这里贴了,嘿嘿
      

  8.   

    [email protected]
    给我发一下吧,急用,谢谢了
      

  9.   

    EachRowEditor.javaimport java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;public class EachRowEditor implements TableCellEditor {
      protected Hashtable editors;  //哈希表
      protected TableCellEditor editor, defaultEditor;  //编辑器
      JTable table;  //这个变量似乎没用到  public EachRowEditor(JTable table) {
        this.table = table;
        editors = new Hashtable();
        defaultEditor = new DefaultCellEditor(new JTextField());  //用JTextField构造编辑器
      }  public void setEditorAt(int row, TableCellEditor editor) {  //关键
        editors.put(new Integer(row),editor);  //指定编辑器加入哈希表
      }  public Component getTableCellEditorComponent(JTable table,
          Object value, boolean isSelected, int row, int column) {    return editor.getTableCellEditorComponent(table,
                 value, isSelected, row, column);
      }  public Object getCellEditorValue() {
        return editor.getCellEditorValue();
      }
      public boolean stopCellEditing() {
        return editor.stopCellEditing();
      }
      public void cancelCellEditing() {
        editor.cancelCellEditing();
      }
      public boolean isCellEditable(EventObject anEvent) {
        selectEditor((MouseEvent)anEvent);
        return editor.isCellEditable(anEvent);
      }
      public void addCellEditorListener(CellEditorListener l) {
        editor.addCellEditorListener(l);
      }
      public void removeCellEditorListener(CellEditorListener l) {
        editor.removeCellEditorListener(l);
      }
      public boolean shouldSelectCell(EventObject anEvent) {
        selectEditor((MouseEvent)anEvent);
        return editor.shouldSelectCell(anEvent);
      }  protected void selectEditor(MouseEvent e) {  //这个没看明白
        int row;
        if (e == null) {
          row = table.getSelectionModel().getAnchorSelectionIndex();
        } else {
          row = table.rowAtPoint(e.getPoint());
        }
        editor = (TableCellEditor)editors.get(new Integer(row));
        if (editor == null) {
          editor = defaultEditor;
        }
      }
    }
    设置用以下方法:
    EachRowEditor rowEditor = new EachRowEditor(JTable);  //JTable那个参数似乎没用,null应该也可以
    rowEditor.setEditorAt(行号, new DefaultCellEditor(jComboBox1));  //用jComboBox1构造编辑器,并指定行
        jTable1.getColumnModel().getColumn(列号).setCellEditor(rowEditor);  //设置编辑器
    上面的"行号","列号"为int型,两个int便指定了你想要的单元格