简体   繁体   English

如何通过JTable中的JComboBox获取选定的值

[英]How to get the Selected Value by a JComboBox in a JTable

I have created a JTable with a JComboBox CellEditor column. 我用JComboBox CellEditor列创建了一个JTable。 That works fine. 很好 However, when I attempt to retrieve the selected value by the combo box, I am only getting the value that the mouse has selected in a specific row. 但是,当我尝试通过组合框检索选定的值时,我仅获得鼠标在特定行中选择的值。 I can't seem to get the selected value on a per row basis. 我似乎无法按行获取选定的值。 here is my code: 这是我的代码:

  addressList = new ArrayList<>(); 
    for(int currentRow = 0; currentRow < editAddressFrm.getAddressTable().getRowCount(); currentRow++)
    {

            //ADDRESS
    offenderAddress = new MyAddress();


    DefaultCellEditor ownerEditor = (DefaultCellEditor) editAddressFrm.getAddressTable().getCellEditor(currentRow, 1);       
    JComboBox ownerCb =(JComboBox) ownerEditor.getComponent();
    ListItem selectedItem =(ListItem) ownerCb.getSelectedItem();  //HERE IS WHERE I HAVE THE PROBLEM.I AM NOT GETTING THE VALUE FOR THE SPECIFIC ROW

    offenderAddress.setOwnerCode(selectedItem.getID());

    offenderAddress.setLine1(
            editAddressFrm.getAddressTable().getModel().getValueAt(currentRow, 2).toString() 
            );

    offenderAddress.setLine2(
            editAddressFrm.getAddressTable().getModel().getValueAt(currentRow, 3).toString() );

    offenderAddress.setCity(
            editAddressFrm.getAddressTable().getModel().getValueAt(currentRow, 4).toString() );
    offenderAddress.setProvince(
            editAddressFrm.getAddressTable().getModel().getValueAt(currentRow, 5).toString() );
    offenderAddress.setPostalZone(
            editAddressFrm.getAddressTable().getModel().getValueAt(currentRow, 6).toString() );
    }

The comboBox is only used to edit the cell. comboBox仅用于编辑单元格。 It does not contain the data for the cell. 它不包含该单元格的数据。

You need to get the data from the table which is done by using: 您需要使用以下方法从表中获取数据:

table.getValueAt(...);

or 要么

table.getModel().getValueAt(...);

depending on your exact requirement 根据您的确切要求

If you are still editing the cell then the data may not be saved to the model yet. 如果仍在编辑单元格,则数据可能尚未保存到模型中。 To solve this problem see: Table Stop Editing . 要解决此问题,请参阅: 表停止编辑

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

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