从jtable中删除行

编程入门 行业动态 更新时间:2024-10-22 14:33:03
本文介绍了从jtable中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想以摇摆形式从jtable中删除一行

i want to remove a row from jtable in swing form

Jtable >>自动从Netbeans摆动(Netbeans 8)拖动

Jtable >> dragged autmatically 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数据>>从MySQL数据库自动绑定

Jtable data >> bind auomatically from MySQL database

我想从jtable中删除行而不是从数据库中删除

i want to delete the row from the 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(); } } }

推荐答案

在这一行:

jTable1.remove(selectedRow);

这 remove(int index)方法不会按照您的想法执行。它继承自容器类,它是旨在从给定容器中删除组件。

This remove(int index) method doesn't do what you think it does. It is inherited from Container class and it is intended to remove components from a given container.

而不是你需要使用 TableModel 并从中删除所选行。由于您使用 matisse (NetBeans的GUI Builder)然后附加到您的表的表模型将是 DefaultTableModel ,因此您可以执行以下操作:

Instead of that you need to work with the TableModel and remove the selected row from it. Since you are using matisse (NetBeans' GUI Builder) then the table model attached to your table will be an instance of DefaultTableModel, so you can do as follows:

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); } }

请查看:

  • 如何使用表格
  • JTable#convertRowIndexToModel(int rowIndex)
  • DefaultTableModel #removeRow(int rowIndex)
  • How to Use Tables
  • JTable#convertRowIndexToModel(int rowIndex)
  • DefaultTableModel#removeRow(int rowIndex)

更多推荐

从jtable中删除行

本文发布于:2023-10-23 13:55:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jtable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!