简体   繁体   English

将选定的Jtable行添加到列表模型

[英]Adding Selected Jtable row to a list model

I want this code to add one transaction to my list model when I select a row but it instead adds two transactions. 当我选择一行时,我希望此代码将一个事务添加到我的列表模型中,而是添加两个事务。 Here is the code: 这是代码:

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);

        }
    });

A ListSelectionListener has an "is adjusting" property" that you need to check to make sure the selection has finished. ListSelectionListener具有“正在调整”属性,您需要检查该属性以确保选择完成。

Read the section from the Swing tutorial on How to Write a ListSelectionListener for more information and working examples on how to test for this. 阅读Swing教程中有关如何编写ListSelectionListener的部分, 获取更多信息以及有关如何进行此测试的示例。

Or you can search the forum for other examples using a ListSelectionListener. 或者,您可以使用ListSelectionListener在论坛上搜索其他示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM