EAS_添加监听事件

1.文本控件监听(KDTextField)

例如有文本控件:

private KDTextField txtBillPackageNo;

 

对于文本监听器,不像其他控件可以直接 txtBillPackageNo.addxxxx来添加监听,我们可以通过不同的方法来实现,有以下几种

第一种:通过监听鼠标的操作来实现监听,再对应的方法中添加自己想要处理的事件

        this.txtBillPackageNo.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //鼠标按键在组件上单击 (按下并释放) 时调用
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                transferDraftNumber();
                //鼠标进入到组件上时调用
            }
            @Override
            public void mouseExited(MouseEvent e) {
                transferDraftNumber();
                //鼠标离开组件时调用
            }
            @Override
            public void mousePressed(MouseEvent e) {
                //鼠标按键在组件上按下时调用
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                //鼠标按钮在组件上释放时调用
            }
        });

第二种:通过DocumentListener()监听器,addDocumentListener 是一个用于监听文档(document)变化的方法。它通常与文本编辑组件(如文本框、文本区域等)一起使用,并用于捕获文本的插入、删除和属性变化等事件。具体使用如下

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class DocumentListenerExample {
    public static void main(String[] args) {
        KDTextField textField = new KDTextField();
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                // 处理文本插入事件
            }

            public void removeUpdate(DocumentEvent e) {
                // 处理文本删除事件
            }

            public void changedUpdate(DocumentEvent e) {
                // 处理文本属性变化事件
            }
        });
    }
}

第三种,也可以用addFocusListener,addFocusListener 是一个用于监听组件焦点变化的方法。它可以用于捕获当用户在界面上选择或取消选择特定组件时的事件。

txtBillPackageNo.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                // 处理获取焦点事件
            }

            @Override
            public void focusLost(FocusEvent e) {
                // 处理失去焦点事件
                transferDraftNumber();
            }
        });

2.分录(KDTable)编辑监听事件

对于分录单元格编辑事件,我们有多种方方法,可以监听整个表格,也可以监听单个单元格,具体如下

2.1监控整个分录表编辑

this.tblMain.addKDTEditListener(new KDTEditAdapter()
    {
      public void editStopping(KDTEditEvent e)
      {
          int colIndex = e.getColIndex();
          int rowIndex = e.getRowIndex();
          String changeValue = e.getValue() ==null?"":e.getValue().toString();
          //票据包号改变处理
          if (colIndex == 2){
              String draftNumber = transferDraftNumber(changeValue,rowIndex,colIndex,1);
              ReceivableBillBatchInputUI.this.tblMain.getRow(rowIndex).getCell("draftNumber").setValue(draftNumber);
          }
          
        //票据包号改变处理
          if (colIndex == 3){
              String draftNumber = transferDraftNumber(changeValue,rowIndex,colIndex,2);
              ReceivableBillBatchInputUI.this.tblMain.getRow(rowIndex).getCell("draftNumber").setValue(draftNumber);
          }
          
          
        //出票人账号改变处理
        if (e.getColIndex() == 8) {
          try
          {
            if ((e.getValue() instanceof CustomerCompanyBankInfo))
            {
              CustomerCompanyBankInfo ccb = (CustomerCompanyBankInfo)e.getValue();
              final int row = e.getRowIndex();
              final String bankAccount = ccb.getBankAccount();
              SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                  ReceivableBillBatchInputUI.this.tblMain.getRow(row).getCell("drawerAccount").setValue(bankAccount);
                }
              });
              ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(ccb.getBank());        
              EntityViewInfo view = new EntityViewInfo();
              FilterInfo filter = new FilterInfo();
              filter.getFilterItems().add(new FilterItemInfo("name", ccb.getBank() + "%", CompareType.LIKE));
              view.setFilter(filter);
              BEBankCollection coll = BEBankFactory.getRemoteInstance().getBEBankCollection(view);
              if (coll.size() > 0) {
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(coll.get(0).getNumber());
              } else {
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(null);
              }
            } else if ((e.getValue() instanceof SupplierCompanyBankInfo)){
              SupplierCompanyBankInfo scb = (SupplierCompanyBankInfo)e.getValue();             
              final int row = e.getRowIndex();
              final String bankAccount = scb.getBankAccount();
              SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                  ReceivableBillBatchInputUI.this.tblMain.getRow(row).getCell("drawerAccount").setValue(bankAccount);
                }
              });
              ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(scb.getBank());              
              EntityViewInfo view = new EntityViewInfo();
              FilterInfo filter = new FilterInfo();
              filter.getFilterItems().add(new FilterItemInfo("name", scb.getBank() + "%", CompareType.LIKE));
              view.setFilter(filter);
              BEBankCollection coll = BEBankFactory.getRemoteInstance().getBEBankCollection(view);
              if (coll.size() > 0) {
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(coll.get(0).getNumber());
              } else {
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(null);
              }
            }
            else if ((e.getValue() instanceof AccountBankInfo)){
              AccountBankInfo acctbank = (AccountBankInfo)e.getValue();              
              final int row = e.getRowIndex();
              final String bankAccount = acctbank.getBankAccountNumber();
              SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                  ReceivableBillBatchInputUI.this.tblMain.getRow(row).getCell("drawerAccount").setValue(bankAccount);
                }
              });
              ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(acctbank.getBank());
              if (acctbank.getBank().getAccountBank() == null){
                BankInfo bankInfo = BankFactory.getRemoteInstance().getBankInfo(new ObjectUuidPK(acctbank.getBank().getId()));
                EntityViewInfo view = new EntityViewInfo();
                FilterInfo filter = new FilterInfo();
                String name = bankInfo.getName();
                filter.getFilterItems().add(new FilterItemInfo("name", name + "%", CompareType.LIKE));
                view.setFilter(filter);
                BEBankCollection coll = BEBankFactory.getRemoteInstance().getBEBankCollection(view);
                if (coll.size() > 0) {
                  ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(coll.get(0).getNumber());
                } else {
                  ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(null);
                }
              }
              else
              {
                BEBankInfo beInfo = BEBankFactory.getRemoteInstance().getBEBankInfo(new ObjectUuidPK(acctbank.getBank().getAccountBank().getId()));
                
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(beInfo.getNumber());
              }
            }
            else if ((e.getValue() instanceof String))
            {
              final int row = e.getRowIndex();
              final String bankAccount = e.getValue().toString();
              SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                  ReceivableBillBatchInputUI.this.tblMain.getRow(row).getCell("drawerAccount").setValue(bankAccount);
                }
              });
              EntityViewInfo view = new EntityViewInfo();
              FilterInfo filter = new FilterInfo();
              filter.getFilterItems().add(new FilterItemInfo("name", e.getValue() + "%", CompareType.LIKE));
              view.setFilter(filter);
              BEBankCollection coll = BEBankFactory.getRemoteInstance().getBEBankCollection(view);
              if (coll.size() > 0) {
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(coll.get(0).getName());
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(coll.get(0).getNumber());
              }else{
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(null);
                ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(null);
              }
            }
            else{
              ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("prmtdrawerBank").setValue(null);
              ReceivableBillBatchInputUI.this.tblMain.getRow(e.getRowIndex()).getCell("drawerBankNum").setValue(null);
            }
          }
          catch (EASBizException e1)
          {
            e1.printStackTrace();
          }
          catch (BOSException e2)
          {
            e2.printStackTrace();
          }
        }
        super.editStopping(e);
      }
    });

第二种,监听单元格

 

posted @ 2023-07-19 14:43  凉了记忆  阅读(327)  评论(0编辑  收藏  举报