I am trying to update database by editing cell values in jtable. my table looks like a

When i edit the value in particular cell and without clicking anywhere if i click on Update button the database is not updated as like this !
but if i click somewhere in the updating row, which again turns the look of table as fig. 1 then the database is upodated with particular value, why is this happening please help. My code for refresh Button goes like this:

        int pos = recordTable.getSelectedRow();
        int id = (Integer) recordTable.getModel().getValueAt(pos, 0);

        ListSelectionModel selectionModel = recordTable.getSelectionModel();
        selectionModel.setSelectionInterval(pos, pos);

        String roomName = (String) recordTable.getValueAt(pos, 1);
        int roomCharge = Integer.parseInt(recordTable.getModel().getValueAt(pos, 2).toString());
        System.out.println(id + roomName + roomCharge);
        admin.updateRoomType(id, roomName, roomCharge);

please help!!

Recommended Answers

All 5 Replies

Sorry my English is not very good, but I use Netbeans to retrieve information from the cell use this code:

 DefaultTableModel dtm1 = (DefaultTableModel) jTable1.getModel();
 dtm1.getValueAt(i, 2).toString();

Not working!!

not working!!

commented: rude +0

Are you sure about the database update? Maybe your update button is updating the database, but there's no code to update the JTable at that point, so you don't see that it's updated? Then when you do anything that causes a repaint of the JTable you see the new data?

The code I posted is to take the data of JTable, so you can show on a JOptionPane.ShowMessage so you will see if the code is correct. After inserting it in the database with SQL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.