如题.
我的部分代码如下:private abstract class TemplateBase implements ActionListener {
final DefaultTableModel model;
TemplateBase(final DefaultTableModel model)
{
this.model = model;
}
final public void actionPerformed(ActionEvent e) {
for( int i =model.getRowCount() - 1 ; i >= 0 ; i-- ){
        model.removeRow(i); }
try {
Statement h = Sqlconn.getStatement();
ResultSet rs = h.executeQuery(getSQL());
                 if(!rs.next()){
                  {JOptionPane.showMessageDialog(null, "没有相关数据!!");}                 }
                 Statement h1 = Sqlconn.getStatement();
  ResultSet rs1 = h1.executeQuery(getSQL());
while (rs1.next()) {
String num = rs1.getString(1);
String code = rs1.getString(2);
String time = rs1.getString(3);
String name = rs1.getString(4);
String guige = rs1.getString(5);
String shuliang = rs1.getString(6);
String danjia = rs1.getString(7);
String jine = rs1.getString(8);
String danwei = rs1.getString(9); String[] str_row = { num, code, time, name, guige,
shuliang, danjia, jine ,danwei};
model.addRow(str_row);// 将一行的数据存在str_row 字符串数组里 }
h.close();
Sqlconn.closeDbConn(); } catch (SQLException e1) {
e1.getStackTrace();
}
ActionListener listener;
jb_print.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (model.getRowCount()==0){JOptionPane.showMessageDialog(null, "没有相关数据,不能打印");}
else{
jb_print.removeActionListener(this);
         getPrint();
}
}
});

}

protected abstract String getSQL();
protected abstract void getPrint();
}
因为我不是每次都要用到这个jb_print.addActionListener(new ActionListener()这个监听,所以我要在用到这个监听之前,把以前用的这个监听给删除,这样我才能保证这有一个监听。

解决方案 »

  1.   

    ActionListener listener=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    if (model.getRowCount()==0){JOptionPane.showMessageDialog(null, "没有相关数据,不能打印");}
    else{
             getPrint();
    }
    }
    };
    int i=0;
    i++;
    System.out.println(i);
    jb_print.addActionListener(listener);
    jb_print.removeActionListener(listener);
    是不是这样啊!!但是这样会把所有的打印监听全都删除了,我想保留最后一次查询后的监听。
      

  2.   

    看了一下,你的问题是多种表格,共用一个打印按钮。1、可以添加一个变量 protected boolean isPaintable = true;对于不许打印的实现,可以设置为false。2、在actionPerformed对打印按钮添加监听做如下处理:
       jb_Print.removeAllActionListener();//对于监听是以List形式保存,将按钮的所有ActionListener先删除。
       对isPaintable进行判断:if(isPaintable){
                                jb_Print.addActionListener(this);
                            }