【问题标题】:How can remove current row in JTable when click JButton?单击JButton时如何删除JTable中的当前行?
【发布时间】:2012-10-09 08:00:27
【问题描述】:

我在JTable 中有很多行,每行都有删除按钮。当我单击该行的删除按钮时,我想删除当前行。我该怎么做?

private JButton button;
public MyTableButtonEditor1() {
    button = new JButton("REMOVE");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DbUtility ViewEmployee =new DbUtility();
            ViewEmployee.loadDriver();
            ViewEmployee.connect();
            ResultSet rs= ViewEmployee.executeDeleteQuery(Employeeid);
            JOptionPane.showMessageDialog(null, "Employee Removed");
        }
    });
} 

数据库连接

public ResultSet  executeDeleteQuery(String Employeeid ) {

    PreparedStatement pstmt ;
    try {

        pstmt = conn.prepareStatement("DELETE FROM employee  WHERE EmployeeId ="+Employeeid  );


        pstmt.execute();
    }
    catch (SQLException ex){
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    }
    return rs;
}

【问题讨论】:

  • 请学习 java 命名约定并遵守它们 - 它会更容易阅读:-)
  • 首先:阅读教程中的示例(在 swing tag wiki 中引用),了解如何将按钮用作 cellEditor 第二:应用所学课程并再次提问(然后答案会更有意义给你)

标签: java swing jdbc jtable


【解决方案1】:

您必须在表格模型中执行此操作。例如,如果你使用javax.swing.table.DefaultTableModel,你可以调用它的removeRow()方法。

【讨论】:

    【解决方案2】:

    来自 Kleoptra 的反馈更新

    触发按钮后,您需要更新编辑器的状态并停止单元格编辑过程。

    public void actionPerformed(ActionEvent e) {
    
        deleteFlag = true;
    
        // This needs to be called that the model and table have a chance to
        // reset themselves...
        stopCellEditing();
    
    }
    

    您需要从编辑器getCellEditorValue返回deleteFlag

    public Object getCellEditorValue() {
        return deleteFlag;
    }
    

    不要忘记在编辑器初始化时重置你的标志。

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        deleteFlag = false;
        // Set up and return your button...
    }
    

    现在在您的模型中,您需要通过覆盖表模型的 setValueAt 方法来捕获事件...

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        switch (columnIndex) {
                case indexOfButtonColumn:
                    if (aValue instanceof Boolean && ((Boolean) aValue).booleanValue()) {
                        // Delete row from database...
                        // Update you internal model.  DefaultTableModel has a removeRow
                        // method, if you're using it.
    
                        // Other wise you will need to update your internal model
                        // and fire the rows delete event in order to update the table...
                        fireTableRowsDeleted(rowIndex, rowIndex);
                    }
                    break;
        }
    }
    

    现在就个人而言,我总是在后台线程或工作线程中执行任何耗时的任务。这将防止 UI “挂起”。

    您可能想阅读Concurrency in Swing 以了解更多信息。

    【讨论】:

    • 假设按钮 is-a cellEditor 上面是该编辑器的 actionPerformed - 不,完全错误。但是假设你的意思不同:-)
    • @kleopatra 我很确定它是TableCellEditor,因为我帮助回答了 OP 关于如何设置它的问题……除了在他删除行时在 EDT 中运行。 ..这次我做错了什么:(
    • 编辑器不得自行更改模型:它的职责是在编辑终止时通知听众,仅此而已。由监听器对该通知采取行动。
    • @kleopatra 感谢您的反馈,它很受重视,我要去用漂白剂洗掉我的眼睛并考虑一下......
    • @kleopatra never_ending 故事 :-) 使用 JButtons 组件作为渲染器、编辑器和 ActionListener、Swing 动作
    【解决方案3】:

    您发布的代码中有几个错误 - 没有 actionPerformed jButton1 和 ListSelectionModel 没有导入。

    看起来您正在使用 NetBeans?可以设置列表选择模型 作为设计时表的属性。作为IDE也应该有 创建了actionPerformed 事件(作为受保护的代码)我不确定那在哪里 走了。

    model.removeRow(rowid); // this line is all you need 
    //table.remove(rowid); <- this line is probably the error 
    

    从模型中移除就足够了 - 您不需要从模型中移除 表组件。我认为这个删除是继承自 java.awt.Component 并试图从表中删除一个组件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 2014-06-21
      • 2011-07-30
      • 2011-12-22
      • 1970-01-01
      • 2019-12-25
      • 2017-05-24
      相关资源
      最近更新 更多