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