Dengues 采用技术(5)-在一个Column下使用多个CellEditor.

一般来说在一个TableColumn里面我们只能设置一个CellEditor,也就是说如果我想改变这个这个TableItem的某Column的CellEditor是不可能的。这样的话我们就只能使用改变这个CellEditor里面的Control来达到目的。
这样的话我们现在Table里面添加一个鼠标按下去的事件。然后在再计算出你要单击的事件是否发生在你要求改变的那个Column里面。如果在的话,就需要你得到一个新的CellEditor。这样的话我们还是主要使用 tableEditor.setEditor()方法来实现。但是使用这种方法要注意的就是:因为你的CellEditor每次都是新的所以你在你改变值的时候就需要,去改变TableItem.getData()对象的值,也就是说你要先改变模型里的值,然后在刷新一下界面。


tableEditor  =   new  TableEditor(table);
        table.addMouseListener(
new  MouseAdapter()  {

            
public void mouseDown(MouseEvent e) {
                Rectangle clientArea 
= table.getClientArea();
                Point pt 
= new Point(e.x, e.y);
                TableItem item 
= table.getItem(pt);
                
if (item != null{
                    
boolean visible = false;
                    
// deactivate the current cell editor
                    if (cellEditor != null && !cellEditor.getControl().isDisposed()) {
                        deactivateCellEditor();
                    }

                    
for (int i = 0; i < table.getColumnCount(); i++{
                        Rectangle rect 
= item.getBounds(i);
                        
if (rect.contains(pt)) {
                            
final int column = i;
                            
if (column == CNUM_DEFAULT) {
                                handleSelect(item);
                            }

                        }

                        
if (!visible && rect.intersects(clientArea)) {
                            visible 
= true;
                        }

                    }

                    
if (!visible) {
                        
return;
                    }

                }

            }


        }
);

private   void  deactivateCellEditor()  {
        tableEditor .setEditor(
nullnull, CNUM_DEFAULT);
        
if (cellEditor != null{
            cellEditor.deactivate();
            
// fireCellEditorDeactivated(cellEditor);
            cellEditor.removeListener(editorListener);
            cellEditor 
= null;
        }

    }


private   void  createEditorListener()  {
        editorListener 
= new ICellEditorListener() {

            
public void cancelEditor() {
                deactivateCellEditor();
            }


            
public void editorValueChanged(boolean oldValidState, boolean newValidState) {
                
// Do nothing
            }


            
public void applyEditorValue() {
                
// Do nothing
            }

        }
;
    }


protected   void  handleSelect( final  TableItem selection)  {
        
// get the new selection
        TableItem[] sel = new TableItem[] { selection };
        
if (sel.length == 0{
            
return;
        }
 else {
            activateCellEditor(sel[
0]);
        }

    }

private   void  activateCellEditor( final  TableItem item)  {
        
// ensure the cell editor is visible
        table.showSelection();

///get new CellEditor.
        cellEditor = DefaultCellEditorFactory.getInstance().getCustomCellEditor();
        
if (cellEditor == null{
            
// unable to create the editor
            return;
        }


        
// activate the cell editor
        cellEditor.activate();
        
// if the cell editor has no control we can stop now
        Control control = cellEditor.getControl();
        
if (control == null{
            cellEditor.deactivate();
            cellEditor 
= null;
            
return;
        }

        
// add our editor listener
        cellEditor.addListener(editorListener);

        
// set the layout of the tree editor to match the cell editor
        CellEditor.LayoutData layout = cellEditor.getLayoutData();
        tableEditor.horizontalAlignment 
= layout.horizontalAlignment;
       
tableEditor .grabHorizontal = layout.grabHorizontal;
       
tableEditor .minimumWidth = layout.minimumWidth;
       
tableEditor .setEditor(control, item, CNUM_DEFAULT);
        
// give focus to the cell editor
        cellEditor.setFocus();

    }

注明:整个Dengues的代码文件太多,无法上传,如果需要源代码。请到用一下方式联系:
QQ群:24885404;Google群:http://groups.google.com/group/dengues.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值