-1

I want to populate the combo box with the value from database based on the selection of table row, it works, but only when I select the different cell of the row then select required combo box cell. But if I select the combobox cell of another row, it shows previous items, and after selecting one of them, if I again click on the combobox it will populate with fresh values. I want the fresh values be populated when I click on row or even combo box cell. Please help. The code is given below:-

String columnName[] = { "Date","Voucher ID","Voucher No","Amount","VOUCHER_NARRATION","Exp. Head" };
String dataValues[][] = {};
voucherTable=new JTable(dataValues,columnName);
voucherTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel list=voucherTable.getSelectionModel();
voucherTable.setSelectionModel(list);
list.addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent lt) {
        try {
            voucherid=voucherTable.getValueAt(voucherTable.getSelectedRow(),1).toString();
            System.out.println(voucherid);
            comboBox.removeAllItems();
            TableColumn road=voucherTable.getColumnModel().getColumn(5);
            voucherTable.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(new VoucherUpdate().getExpHead(voucherid)));
        } catch(Exception h) { 
            System.err.println(h); 
            return;
        }
    }
});

public JComboBox getExpHead(String vid) {
    String voucherId=vid;
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn=DriverManager.getConnection("jdbc:odbc:SARALIFMS","SARALIFMS","O391120SUMAN");
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("SELECT E.EXP_ID,EXP_HEAD FROM TD_VOUCHER_SUMAN D, MM_PRIA_EXPENDITURE E"
        +" WHERE VOUCHER_ID='"+voucherId+"' AND D.ACCOUNT_CODE=E.ACCOUNT_CODE");
        while(rs.next()) {
            String expHead=rs.getString("EXP_HEAD");
            comboBox.addItem(expHead);
        } 
        conn.close();
    } catch(Exception e) {
        System.out.println(e);
    }
    return comboBox;
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    For best help, create and post an [mcve], a very small program that compiles and runs and shows us your problem. It should be a simplified extraction of your current program, should not have database code within it, again should be as simple as possible and yet be runnable for us without our having to alter it. Yes, this is asking work from you, but if you do this then we will easily and quickly be able to understand your problem and thus be able to help you. – Hovercraft Full Of Eels Jul 09 '16 at 11:28
  • 1
    If your question is not a duplicate, please edit your question to include a [mcve] that shows how you tried to use the approach shown. – trashgod Jul 09 '16 at 14:08
  • This question was originally closed as a duplicate of: [How do I get the CellRow when there is an ItemEvent in the JComboBox within the cell](http://stackoverflow.com/q/7350445/131872). This question is about invoking code when the row selection of a table changes. I don't believe it is a duplicate so I reopened the question. – camickr Jul 09 '16 at 15:26

1 Answers1

1

it works, but only when I select the different cell of the row then select required combo box cell

When you click on a cell using a combo box as an editor, the editor is invoked BEFORE the selected row has been updated. So this is a timing issue.

One solution is to override the getCellEditor(...) method of the JTable to load the data when the editor is actually invoked. At this point in time the selected row will be updated and you can get the data for the appropriate row.

This approach is demonstrated in this question: https://stackoverflow.com/a/4211552/131872

Note you may want to consider caching the data so you don't do an SQL access every time you edit the cell.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288