java-从jtable删除行
作者:互联网
我想以摆动形式从jtable中删除一行
Jtable>>从Netbeans摇摆中自动拖动(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数据从MySQL数据库自动绑定
我只想从jtable中删除行而不是从数据库中删除
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();
}
}
}
解决方法:
在这一行:
jTable1.remove(selectedRow);
此remove(int index)方法无法执行您认为的操作.它从Container类继承,旨在从给定容器中删除组件.
相反,您需要使用TableModel并从中删除选定的行.由于您使用的是matisse(NetBeans的GUI Builder),因此连接到表的表模型将是DefaultTableModel的实例,因此您可以执行以下操作:
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);
}
}
请看一下:
> How to Use Tables
> JTable#convertRowIndexToModel(int rowIndex)
> DefaultTableModel#removeRow(int rowIndex)
标签:swing,jtable,netbeans,matisse,java,matisse 来源: https://codeday.me/bug/20191028/1955713.html