JAVA - How To Update A Row From JTable Using JTextfield In Java

JAVA - How To Update JTable Selected Row Using JTextfield In Java

__________________________________________________________________________

In This Java Code We Will See How To Update A Selected Row From JTable  Using 
JTextfield In Java Programming Language With NetBeans.






Source Code:


package java_tutorials;

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

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

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

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

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldID = new javax.swing.JTextField();
        jTextFieldFN = new javax.swing.JTextField();
        jTextFieldLN = new javax.swing.JTextField();
        jTextFieldAGE = new javax.swing.JTextField();
        btnUpdateRow = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel1.setText("Id :");

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel2.setText("First Name :");

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel3.setText("Last Name :");

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jLabel4.setText("Age :");

        btnUpdateRow.setText("Update Row");
        btnUpdateRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateRowActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "DDFFD", "DGHYTGD", "344"},
                {"2", "FGHTYH", "UYUYHF", "33"},
                {"3", "JHGF", "JHGFD", "254"},
                {"4", "WQER", "OUYREE", "38"},
                {"5", "BSGH", "IYDGH", "22"},
                {"6", "QSDTYKJH", "PIHGDX", "16"},
                {"7", "DSZDD", "UTRFRT", "2567"},
                {"8", "XCVBGR", "YTEUI", "56"},
                {"9", "FGHJUY", "NGFDBNJ", "65"},
                {"10", "WQAZR", "VXWG", "42"}
            },
            new String [] {
                "Id", "First Name", "Last Name", "Age"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(27, 27, 27))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnUpdateRow, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldFN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldLN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldAGE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addComponent(btnUpdateRow, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );

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

    private void btnUpdateRowActionPerformed(java.awt.event.ActionEvent evt) {                                          

        int i = jTable1.getSelectedRow();
         DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        if(i >= 0)
        {
            model.setValueAt(jTextFieldID.getText(), i, 0);
            model.setValueAt(jTextFieldFN.getText(), i, 1);
            model.setValueAt(jTextFieldLN.getText(), i, 2);
            model.setValueAt(jTextFieldAGE.getText(), i, 3);
        }else{
            JOptionPane.showMessageDialog(null, "Error");
        }
     
    }                                          

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                  
   
        int selectedRow = jTable1.getSelectedRow();
        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        jTextFieldID.setText(model.getValueAt(selectedRow, 0).toString());
        jTextFieldFN.setText(model.getValueAt(selectedRow, 1).toString());
        jTextFieldLN.setText(model.getValueAt(selectedRow, 2).toString());
        jTextFieldAGE.setText(model.getValueAt(selectedRow, 3).toString());
    }                                  

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        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_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_JTable_Update_Selected_Row_Using_TextBoxes.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_JTable_Update_Selected_Row_Using_TextBoxes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JButton btnUpdateRow;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldAGE;
    private javax.swing.JTextField jTextFieldFN;
    private javax.swing.JTextField jTextFieldID;
    private javax.swing.JTextField jTextFieldLN;
    // End of variables declaration                
}

/////////////////////////////// OUTPUT:


update jtable selected row using jtextfields in java
JTable Selected Row Updated From JTextFields




Another Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class Work extends JFrame{

    JTextField JT_fname,JT_lname,JT_age,JT_id;

    JButton btn_add,btn_update;

    JTable table = new JTable();

    JScrollPane pane;

   Object[] cols = null;

 public Work(){   

     JT_id = new JTextField(20);
     JT_fname = new JTextField(20);
     JT_lname = new JTextField(20);
     JT_age = new JTextField(20);

     JT_id.setBounds(130,20,150,20);
     JT_fname.setBounds(130, 50, 150, 20);
     JT_lname.setBounds(130, 80, 150, 20);
     JT_age.setBounds(130, 110, 150, 20);

     btn_add = new JButton("ADD");
     btn_add.setBounds(300, 30, 100, 20);

     btn_update = new JButton("UPDATE");
     btn_update.setBounds(300,100,100,20);

       cols = new String[]{"id","fname","lname","age"};
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setColumnIdentifiers(cols);

       btn_add.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {

              model.addRow(new Object[]{
                               JT_id.getText(),JT_fname.getText(),
                               JT_lname.getText(),JT_age.getText()
                                       });
                  }
             });
       
       
       // Get Selected Row Values From JTable Into JTextfields
          table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ 
          @Override
          public void valueChanged(ListSelectionEvent e) {
          
                int i = table.getSelectedRow();
                JT_id.setText((String)model.getValueAt(i, 0));
                JT_fname.setText((String)model.getValueAt(i, 1));
                JT_lname.setText((String)model.getValueAt(i, 2));
                JT_age.setText((String)model.getValueAt(i, 3));
            }
        });
       
// Update A  JTable Row Using JTextfield
      btn_update.addActionListener(new ActionListener(){
      
      @Override
      public void actionPerformed(ActionEvent e){
          
          int i = table.getSelectedRow();
          model.setValueAt(JT_id.getText(), i, 0);
          model.setValueAt(JT_fname.getText(), i, 1);
          model.setValueAt(JT_lname.getText(), i, 2);
          model.setValueAt(JT_age.getText(), i, 3);
      }
      });    
       
     pane = new JScrollPane(table);
     pane.setBounds(100,150,300,300);
     setLayout(null);
  
     add(pane);
     add(JT_id);
     add(JT_fname);
     add(JT_lname);
     add(JT_age);
     add(btn_add);
     add(btn_update);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     Color c = Color.decode("#bdb76b");
     getContentPane().setBackground(c);
     setLocationRelativeTo(null);
     setSize(500,500);
    
 }
   public static void main(String[] args){
       new  Work();
   }
}


Share this

Related Posts

Previous
Next Post »