簡體   English   中英

單擊JButton時如何刪除JTable中的當前行?

[英]How can remove current row in JTable when click JButton?

我在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;
}

您必須在表模型中執行此操作。 例如,如果使用javax.swing.table.DefaultTableModel ,則可以調用其removeRow()方法。

來自Kleoptra的Feeback更新

觸發按鈕后,您需要更新編輯器的狀態並停止單元格編輯過程。

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“掛起”。

您可能希望閱讀Swing中並發以獲取更多信息。

您發布的代碼中有幾個錯誤-jButton1沒有actionPerformedListSelectionModel沒有導入。

看來您在使用NetBeans? 您可以在設計時將列表選擇模型設置為表的屬性。 由於IDE也應該已經創建了actionPerformed事件(作為受保護的代碼),所以我不確定該去哪了。

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

從模型中刪除就足夠了-您無需從表組件中刪除。 我認為此刪除是從java.awt.Component繼承的,並且正在嘗試從表中刪除一個組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM