Java JTable Click Event In Netbeans

Java JTable Code -  Java JTable Click Event Using NetBeans .

                                                                           


In this java Tutorial we will see How To Use A JTable Mouse Clicked In Java Using NetBeans
To Display The Selected Row Columns Sum .






- in the mouse click we will get the row selected and get the sum  of the row columns and display it in JOptionPane :

    private void jTable_ClickMouseClicked(java.awt.event.MouseEvent evt) {                                          
       // get the selected row
       int index = jTable_Click.getSelectedRow();

       TableModel model = jTable_Click.getModel();

       int value1 = Integer.parseInt(model.getValueAt(index, 0).toString());
       int value2 = Integer.parseInt(model.getValueAt(index, 1).toString());
       int value3 = Integer.parseInt(model.getValueAt(index, 2).toString());
       int value4 = Integer.parseInt(model.getValueAt(index, 3).toString());
       
       int sum = value1+value2+value3+value4;
       
        JOptionPane.showMessageDialog(null, "Sum = "+sum);
    } 


Entire Project Source Code:

package JAVA_VIDEOS_TUTORIALS;

import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

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

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

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_Click = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable_Click.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"0", "1", "2", "3"},
                {"4", "5", "6", "7"},
                {"8", "9", "10", "10"},
                {"10", "10", "20", "20"},
                {"20", "20", "30", "30"},
                {"50", "50", "40", "40"},
                {"100", "200", "300", "400"},
                {"500", "500", "1000", "2000"}
            },
            new String [] {
                "Value 1", "Value 2", "Value 3", "Value 4"
            }
        ));
        jTable_Click.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_ClickMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_Click);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

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

    private void jTable_ClickMouseClicked(java.awt.event.MouseEvent evt) {                                          
       // get the selected row
       int index = jTable_Click.getSelectedRow();

       TableModel model = jTable_Click.getModel();

       int value1 = Integer.parseInt(model.getValueAt(index, 0).toString());
       int value2 = Integer.parseInt(model.getValueAt(index, 1).toString());
       int value3 = Integer.parseInt(model.getValueAt(index, 2).toString());
       int value4 = Integer.parseInt(model.getValueAt(index, 3).toString());
       
       int sum = value1+value2+value3+value4;
       
        JOptionPane.showMessageDialog(null, "Sum = "+sum);
    }                                      

    /**
     * @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(JTable_Click.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JTable_Click.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JTable_Click.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTable_Click.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 JTable_Click().setVisible(true);
            }
        });
    }

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



///////////////OUTPUT:
java jtable mouse clicked
jtable mouse clicked






Share this

Related Posts

Previous
Next Post »