Remove row from jtable
I want to remove row from jtable in swing form
Jtable -> automatically dragged from Netbeans swing (Netbeans 8)
private javax.persistence.EntityManager entityManager;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private java.util.List<javaapplication1.Orders> ordersList;
private javax.persistence.Query ordersQuery;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
Jtable data -> automatically link MySQL database
I want to delete a row from jtable only not from the database
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int selectedRow = jTable1.getSelectedRow();
if(selectedRow!=-1)
{
try {
jTable1.remove(selectedRow);
jTable1.revalidate();
} catch (Exception e) {
e.getMessage();
}
}
}
+3
source to share
2 answers
In this line:
jTable1.remove(selectedRow);
This remove (int index) method does not do what you think it does. It inherits from the Container class and is designed to remove components from a given container.
Instead, you need to work with the TableModel and remove the selected row from it. Since you are usingmatisse(NetBeans Builder GUI) then the table model attached to your table will be an instance of DefaultTableModel , so you can do the following:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int viewIndex = jTable1.getSelectedRow();
if(viewIndex != -1) {
int modelIndex = jTable1.convertRowIndexToModel(viewIndex); // converts the row index in the view to the appropriate index in the model
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.removeRow(modelIndex);
}
}
Please take a look at:
+4
source to share
Working with JTableBinding:
private JTableBinding<LpDetail, LpMaster, JTable> lpDetailListTableBinding;
you can do this right above pojos:
protected void btnEliminarItemActionPerformed(ActionEvent e) {
int sustract = tb_DetLP.getSelectedRow();
if (sustract >= 0) {
selectedItem.setLpDetailList(org.jdesktop.observablecollections.ObservableCollections
.observableList(selectedItem.getLpDetailList()));
selectedItem.getLpDetalleList().remove(sustract);
}
}
0
source to share