我知道的设置方法是
JTable.getColunmModel.getColum(列号).setCellEditor(编辑器)
这样会使整列都使用给定的编辑器,我只想某一行使用我的编辑器,如何实现?
请给出大概的代码,有人说可以重写
CellEditor类里的
Component getTableCellEditorComponent方法,但不具体,我没试出来
哪位大哥可以帮个忙?

解决方案 »

  1.   

    private class CustomCellEditor extends AbstractCellEditor implements TableCellEditor,ActionListener{
    Color currentColor;
    JButton button;
    JColorChooser chooser;
    JDialog dialog;
    CustomCellEditor(){
    button = new JButton ();
    button.addActionListener( this );
    button.setActionCommand("editor");
    chooser = new JColorChooser();
    dialog = JColorChooser.createDialog( button,"pick a color",true,chooser,this,null);
    }
    public void actionPerformed( ActionEvent e ){
    if( e.getActionCommand().equals( "editor" ) ){
    button.setBackground( currentColor );
    chooser.setColor(currentColor);
    dialog.setVisible( true );
    fireEditingStopped();
    }else{

    currentColor = chooser.getColor();
    }
    }
    public Object getCellEditorValue(){
    return currentColor;
    }
    public Component getTableCellEditorComponent( JTable table,Object value,boolean isSelected,int row,int column){
    currentColor = (Color)value;
    return button;
    }
    }
    ==================================
    这是一个重写CellEditor的例子
    你改一下getTableCellEditorComponent,判断一下那个row和column参数是不是你要的单元格,如果是就返回你要的,不是就返回默认的
    再调用
    JTable.getColunmModel.getColum(列号).setCellEditor(编辑器)
      

  2.   

    你在其他编辑器的getTableCellEditorComponent返回什么,这里就返回什么啦如果因为value没有包含你要的信息而生成不了你要的component,可以写一类把所有信息包装进去不知道我有没有理解错
      

  3.   

    table = new JTable( customModel ){
         public TableCellEditor getCellEditor(int row, int column) {
    if ((row == 1) && (column == 2)) {
          return new CustomCellEditor();
    }
    return super.getCellEditor(row, column);
        }
    };
    要不在这里根据row和column设置
      

  4.   

    table = new JTable( customModel ){
         public TableCellEditor getCellEditor(int row, int column) {
    if ((row == 1) && (column == 2)) {
          return new CustomCellEditor();
    }
    return super.getCellEditor(row, column);
        }
    };
    这样的话编辑器就写死了,而不能根据需要改变,我把我做的东东描述一下吧:
    表格同一行里,一个下拉菜单的选项改变,另一格里的下拉菜单将换成对应选项
    例如我选"车辆",同一行另一格里的下拉菜单是"卡车","轿车","单车"
    如果我选"水果",那里将变成"香蕉","苹果","哈密瓜"
    我的实现想法是当表格改变,得到改变的行号,设置新的下拉菜单作为编辑器,其他的都搞定了,就卡在设置编辑器上
    大哥你有更好的想法请指教,麻烦你了:)
      

  5.   

    DefaultCellEditor editor = (DefaultCellEditor)= table.getCellEditor(int row, int column) ;JComboBox comboBox = (JComboBox)editor.getComponent();
    然后对comboBox做文章
      

  6.   

    你上贴说:你只是内容不同对不对?   是的.
    DefaultCellEditor editor = (DefaultCellEditor)table.getCellEditor(int row, int column);
    JComboBox comboBox = (JComboBox)editor.getComponent();
    我试了这个方法,有效果,但是我在一行选"车辆",使得另一行"水果"对应的也变成"卡车","轿车","单车"了.估计是因为整列使用同一个JComboBox来构建.
    要解决这个问题可能每行都得使用自己的JComboBox,这怎么做呢?
      

  7.   

    我的QQ176457718, 可以的话我们QQ上聊比较快, 好吗?