How to Delete a Row record of jtable in java
This tutorial is to teach you how you can delete a row record of jtable in java netbeans with UI and source codes.
After you have created your jtable and populated it with records like the image bel
You can create a delete button to perform the delete process for you.
In the above interface, our jtable contents some list of records, about four records.
Now we want to delete the last row, with ID "9".
To do that, we have to;
- Select the row we want to delete. like i have done in the image below;
- After the row selection, click on the red delete button.
3. You will get a pop-up like the image below asking you for confirmation.
If you click on the "Yes" button, the record would be deleted.
else if you click on the "No" button, the process would be cancelled.
4. When the delete process is proceeded by clicking on the "Yes" button the record would be deleted as show in the image below;
You can see that the last record which was selected with an ID number "9" has been deleted from the records.
Below is the code under the delete button;
/*******************************************************************************
// deleting record in the table list and updating item quantity
DefaultTableModel model = (DefaultTableModel) jTableInvoice01.getModel();
int Index =jTableInvoice01.getSelectedRow();// row number is assigned to a variable index
if (Index ==-1){
if (Index ==0){
JOptionPane.showMessageDialog(null,"No Data to Delete", "RuDon Roofing sytem", JOptionPane.OK_OPTION);
}else{
JOptionPane.showMessageDialog(null,"Select a Row to Delete", "RuDon Roofing sytem", JOptionPane.OK_OPTION);
}
}else{
String ID = model0.getValueAt(Index,0).toString();
String qty = model.getValueAt(Index,2).toString();
double qty1=Double.parseDouble(model0.getValueAt(Index,2).toString());
int opt = JOptionPane.showConfirmDialog(null,"Are you sure to Delete", "Delete sytem", JOptionPane.YES_NO_OPTION);
if (opt==0){
try{
//Class.forName("org.apache.derby.jdbc.ClientDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//String connect="jdbc:derby://localhost:1527/RuDonDB";
String connect="jdbc:ucanaccess://C:/Users/jrs/RuDon/Roofing/System/db/temp/NhyiraCosmeticDB.accdb";
con=DriverManager.getConnection(connect);
// JOptionPane.showMessageDialog(null,"Connec
String DBQ ="SELECT ID, Quantity from wholeProduct";
Statement s=con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
rslt=s.executeQuery(DBQ);
while(rslt.next()){
if(rslt.getString("ID").equals(ID)){
double qty2=Double.parseDouble(rslt.getString("Quantity"));
rslt.updateString(2, String.format("%.2f",qty1+qty2 ));
rslt.updateRow();
}else{
}
}
}catch(ClassNotFoundException | SQLException | HeadlessException ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
/********************************************************************************
Below image is the beauty of it..
I hope this tutorial finds you useful.
Thank you for reading.
You can comment about anything concerning this process and code.
1 Comments
Great piece
ReplyDeleteDear reader, if you think of a different or an alternative way to achieve this solution, kindly share you idea in the comment section. Thank you.