public synchronized void run(){
if (SwingUtilities.isEventDispatchThread()) { //AWT指派线程
System.out.println("---收到数据 回来-----");
String [] columnNames={" ","是否选择","逻辑条形码","商品名称","规格","单位","销售单价","颜色","颜色代码","条形码"};
Object [][]obj={
{"1",new Boolean(false),"01001011000","商品名称商品名称","12*1","包","123.5","红色","02","121211"},
{"2",new Boolean(false),"01001011050","商品名称商品名称","12*1","包","12","粉红色","03","121211"},
{"3",new Boolean(false),"01001011040","商品名称商品名称","12*1","包","9.5","棕色","25","121211"},
};
//改变 Into.jtable
Into.jtable.setModel(new MyTableModel(obj,columnNames));
Into.jtable.setEnabled(false);
//设置监听行
selectionMode=Into.jtable.getSelectionModel();
selectionMode.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
int rows=Into.jtable.getSelectedRow(); //取得选中的第几行
if(rows>-1 && e.getValueIsAdjusting()){
Boolean object=(Boolean)Into.jtable.getValueAt(rows,1);//取得单元格的值
Into.jtable.setValueAt(new Boolean(!object), rows,1);
}
}
});
Into.jtable.setEnabled(true);
}else{
SwingUtilities.invokeLater(this);
}
}
sokcet收到数据后,执行这个线程,但是,第一次收到数据,执行一次ListSelectionListener里面的两次valueChanged,第二次收到数据,执行两次ListSelectionListener里面的四次valueChanged,收到第三次数据,执行三次ListSelectionListener里面的八次valueChanged,如此类推。。。。
知道每选中Jtable一行,都执行一次ListSelectionListener里面的两次valueChanged,但是怎么会跟收到数据的次数有关?哪错了?
第二次收到数据时,没有点选JTable,ListSelectionListener也执行了
run方法每次执行时都执行这句代码
selectionMode.addListSelectionListener
这样每执行一次就注册一个监听器,soso~~~
把注册监听的代码移出去,只注册一次就好了。
是的,解决问题了