现有如下几个组件,JTable,JComboBox
在JTable中加入了一列JComboBox,table.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(new DP_ComboBox(items,"0")));加载数据后model.addRow(new Object[] {"A0001", "AUName", "AUUserID", "AUPasswd", "AUType", new DP_ComboBox(items,"0"), "AUDF", "AURegion"});其他组件说明:
model为一个AbstractTableModel类;
DP_ComboBox为一个自定义的JComboBox,其中实现了类似WEB中<select>的效果,即有一个text属性和一个value属性,显示的为text,运用过程中取实际的value值但现在发现如下的一个问题:
向JTable中加载数据后JComboBox中显示的数据正确,但是如果选取一个非最后行的JComboBox时,发现默认值变成了在设置cellEditor时的值了,而不是默认从数据库中读出的值。
用此例来说,在设置cellEditor时,设置的是new DP_ComboBox(items,"0"),即JComboBox的默认值为0,显示为“否”,但如果经过如下加载数据后model.addRow(new Object[] {"A0001", "AUName", "AUUserID", "AUPasswd", "AUType", new DP_ComboBox(items,"1"), "AUDF", "AURegion"});
model.addRow(new Object[] {"A0001", "AUName", "AUUserID", "AUPasswd", "AUType", new DP_ComboBox(items,"0"), "AUDF", "AURegion"});这样在JTable中就有了两行数据,但由于第二行设置数据的JComboBox默认值为0和setCellEditor一致,所以看不出区别,但点了第一行之后默认值却由原来的1变成了0,即根据数据库加载的数据第一行JComboBox的默认值为1,但由于setCellEditor设置时的值为0,点了这个JComboBox后即便是不选择任何一项,显示的默认值都会变成0。不知道有没有朋友遇到过这样的现象或者是有解决的方法,说的有点乱,不知道大家看明白了没有。问题比较急,希望能尽快解决,在此先谢谢各位啦。

解决方案 »

  1.   

    学习,我还没在JTable中加过JComboBox,连自定义JComboBox都没搞过。java的swing麻烦,不好用。
      

  2.   

    table.getColumnModel().getColumn(5).setCellEditor(new   DefaultCellEditor(new   DP_ComboBox(items, "0 "))); 
    这样设置的话CellEditor实例只有一个。
    重载JTable的public TableCellEditor getCellEditor(int i, int j){
       //跟据实际情况动态地返回CellEditor
       String _sDefault =  _aDefaultValue[i][j] ;
       return new   DefaultCellEditor(new   DP_ComboBox(items, _sDefault));
      

  3.   

    to sdd330 大概的我也了解一点
    那如何在向JTable或TableModel中添加数据的时候就把新的实例加到指定的Cell里呢?
    现在关键的问题就在如何加新实例到表中,还有一个就是如何取出表中的实例
      

  4.   

    public   TableCellEditor   getCellEditor(int   i,   int   j){
    if(model.getValueAt(i, j) instanceof DP_ComboBox){
    return   new       DefaultCellEditor((DP_ComboBox)model.getValueAt(i, j));
    }
    }
      

  5.   

    你这个的Renderer和Editor都需要重写重写DefaultCellEditor:
    public Component getTableCellEditorComponent(JTable table, Object value,
     boolean isSelected,
     int row, int column) {
            Component c = super.getTableCellEditorComponent();
            if (c instanceof DP_ComboBox) {
    // 根据参数值value设置DP_ComboBox的值
    }
    return c;
        }实现自己的TableCellRenderer:
    class DP_ComboBoxRenderer implements DefaultTableCellRenderer {
    private DP_ComboBox dp;
     Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)  {
    //TODO: 根据value值设置dp的值
    return dp;
     }
    }然后把Editor和 Render设给5列你这个程序还需要解决value是什么形式的问题, 没有必要非把DP_ComboBox传给table,只需要把它需要的值传过来就行了.
    就像在显示其它列数据的时候是一个JLabel, table相应的列只需要String型的数据就可以了,不用每一个Cell都传一个 JLabel给它.
    不用这样:
    model.addRow(new   Object[]   { "A0001 ",   "AUName ",   "AUUserID ",   "AUPasswd ",   "AUType ",   new   DP_ComboBox(items, "0 "),   "AUDF ",   "AURegion "}); 
    可以这样:
    model.addRow(new   Object[]   { "A0001 ",   "AUName ",   "AUUserID ",   "AUPasswd ",   "AUType ",   new   DPData(items, "0 "),   "AUDF ",   "AURegion "}); 
    DPData是DP_ComboBox存的数据不知道我写的能不能让楼主觉得清楚.建议你看一下JTable的Render和Editor机制.
      

  6.   

    重新排了一下:你这个的Renderer和Editor都需要重写重写DefaultCellEditor:
    public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected,
                                     int row,  int column)   {
                    Component c = super.getTableCellEditorComponent(table,value,isSelected,row,column);
                    if   (c instanceof DP_ComboBox) {
                        //TODO:根据参数值value设置DP_ComboBox的值
                     }
                    return   c;
            }实现自己的TableCellRenderer:
    class DP_ComboBoxRenderer implements DefaultTableCellRenderer {
          private DP_ComboBox dp;
          Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                           int column) {
                //TODO:根据value值设置dp的值
                return   dp;
          }
    }然后把Editor和 Renderer设给5列你这个程序还需要解决value是什么形式的问题,没有必要非把DP_ComboBox传给table,只需要把它需要的值传过来就行了.
    就像在显示其它列数据的时候是一个JLabel, table相应的列只需要String型的数据就可以了,不用每一个Cell都传一个JLabel给它.
    不用这样:
    model.addRow(newObject[]{"A0001","AUName", "AUUserID","AUPasswd", "AUType", new DP_ComboBox(items,"0"),  "AUDF",       "AURegion"});  
    可以这样:
    model.addRow(new Object[] {"A0001", "AUName", "AUUserID","AUPasswd","AUType",  newDPData(items,"0"),"AUDF","AURegion"});  
    DPData是DP_ComboBox存的数据不知道我写的能不能让楼主觉得清楚.建议你看一下JTable的Render和Editor机制.
    ps: 也不知道写全了没,你测试一下吧.
      

  7.   

    这个是CellEditor的问题吧,在编辑状态下的,Render就不需要重写了