JAVA - How To Remove A Selected Row From JTable In Java


JAVA - How To Delete A Selected Row From JTable In Java Using NetBeans

________________________________________________________________________

In This Java Code We Will See How To Remove The Selected Row From A  JTable In Java Programming Language.



Java Source Code:

package java_tutorials;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 1bestcsharp.blogspot.com
 */
public class Java_Remove_JTable_Selected_Row extends javax.swing.JFrame {

    /**
     * Creates new form Java_Remove_JTable_Selected_Row
     */
    public Java_Remove_JTable_Selected_Row() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton_Delete_Row = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "dfsrtu", "rfgfdfgtr", "111"},
                {"2", "cdhyyuyu", "tfvtu", "31"},
                {"3", "ryytgtyu", "qsyuioo", "22"},
                {"4", "piuytht", "nrtil", "63"},
                {"5", "stghtrf", "zerggf", "44"},
                {"6", "zyi_oh", "qruikio", "21"}
            },
            new String [] {
                "Id", "First Name", "Last Name", "Age"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton_Delete_Row.setText("Remove Selected Row");
        jButton_Delete_Row.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Delete_RowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(169, 169, 169)
                        .addComponent(jButton_Delete_Row)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(45, Short.MAX_VALUE)
                .addComponent(jButton_Delete_Row, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>                      

// button delete row
    private void jButton_Delete_RowActionPerformed(java.awt.event.ActionEvent evt) {                                                   
       DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

       // get selected row index

       try{
       int SelectedRowIndex = jTable1.getSelectedRow();
       model.removeRow(SelectedRowIndex);
       }catch(Exception ex)
       {
           JOptionPane.showMessageDialog(null, ex);
       }
    }                                                

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_JTable_Selected_Row.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_JTable_Selected_Row.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_JTable_Selected_Row.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_JTable_Selected_Row.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java_Remove_JTable_Selected_Row().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JButton jButton_Delete_Row;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                
}

OutPut :
delete jtable selected row
JTable Remove Selected Row

Share this

Related Posts

Previous
Next Post »