当我选择一行时,我希望此代码将一个事务添加到我的列表模型中,但它却添加了两个事务。这是代码:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent event) {
String name = (String) table.getValueAt(table.getSelectedRow(), 0);
Double price = Double.valueOf((String) table.getValueAt(table.getSelectedRow(), 1));
String transaction = name + " " + price;
myModel.addElement(transaction);
list.setModel(myModel);
}
});
最佳答案
ListSelectionListener
有一个“正在调整”属性,您需要检查该属性以确保选择已完成。
阅读 Swing 教程中关于 How to Write a ListSelectionListener 的部分有关如何对此进行测试的更多信息和工作示例。
或者您可以使用 ListSelectionListener 在论坛中搜索其他示例。
关于java - 将选定的 Jtable 行添加到列表模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33602429/