views:

109

answers:

0

I have a table (JTable) that has a comboBox in one of it columns. Say the combobox is consist of id. So, whenever user choose from comboBox (in a selected row), the next column in the table will display the name of the id (in the same selected row). I tried this code:

       cboItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Statement stmt;
            ResultSet rs;
            try {
                //generate code
                sql = "SELECT desc_bar FROM inv_item WHERE kode_bar = '" +
                        cboItem.getSelectedItem().toString() + "'";
                stmt = Koneksi.getConnection().createStatement();
                rs = stmt.executeQuery(sql);

                if (rs.last()){
                    table.setValueAt(rs.getString(1), table.getSelectedRow(), 1);
                } else {
                    table.setValueAt("", table.getSelectedRow(), 1);
                }
                rs.close();
                stmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(TrPOEntry.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

It got an exception which is index out of bound. This is the one that caused the problem:

table.setValueAt(rs.getString(1), table.getSelectedRow(), 1);