JAVA - How To Add A Row To JTable From JTextfields in Java

jtable add row

JAVA - How To Add A Row To JTable Using JTextfields In Java NetBeans

________________________________________________________________________

In This Java Code We Will See How To Insert A Row In JTable From JTextfield  In Java Programming Language.



Source Code:

package java_tutorials;

import javax.swing.table.DefaultTableModel;

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

    /**
     * Creates new form Java_JTable_Add_Row_Using_TextBoxes
     */
    public Java_JTable_Add_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();
        btnAddRow = 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 :");

        btnAddRow.setText("Add Row");
        btnAddRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddRowActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "First Name", "Last Name", "Age"
            }
        ));
        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.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.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnAddRow, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, 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(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            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))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAddRow, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

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

    // button to add the row to the jtable 
    private void btnAddRowActionPerformed(java.awt.event.ActionEvent evt) {                                          
       // get the model from the jtable
        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();

         // insert row to the model from jtextfields using addRow method
        model.addRow(new Object[]{jTextFieldID.getText(), jTextFieldFN.getText(),
                                  jTextFieldLN.getText(), jTextFieldAGE.getText()});

        
    }                                      

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

    // Variables declaration - do not modify                  
    private javax.swing.JButton btnAddRow;
    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:


Java Add A Row To JTable From JTextfields
 Row Added To JTable From JTextfields




Another Source Code:

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


public class Work extends JFrame{
              JTextField JT_fname,JT_lname,JT_age,JT_id;
              JButton btn_add;
              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, 80, 100, 20);

       cols = new String[]{"id","fname","lname","age"};

            DefaultTableModel model = (DefaultTableModel) table.getModel();

            model.setColumnIdentifiers(cols);

      //Add A Row To JTable From JTextfields
       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()
                                       });
        }
 });
     
       
       
     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);
     
     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();
   }
}
      ///////////////////////////////OUTPUT:
How To Add A Row To JTable From JTextfields in Java
Row Inserted To JTable From JTextfields






VB.NET - How To Add Delete And Update DataGridView Row Using TextBoxes In VBNET

VB.NET - How To Add Delete And Update Row From DataGridView Using TextBoxes In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Add A Row To DataGridView From TextBox, Update A Selected Row Using TextBox, And Delete The Selected Row In VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Add_Update_Delete_DataGridView_Row

 ' Create a new datatable
    Dim table As New DataTable("Table")

    Dim index As Integer

    Private Sub Add_Update_Delete_DataGridView_Row_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Add columns to your datatable, 
    ' with the name of the columns and their type 
        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

         ' Add rows to the datatable with some data
        table.Rows.Add(1, "XXXX", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        table.Rows.Add(7, "aefht", "cvfhytrff", 21)
        table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
        table.Rows.Add(9, "qsztii", "bvdhjh", 53)
        table.Rows.Add(10, "rytyufd", "esdfzr", 19)

   'set data from datatable to datagridview
        DataGridView1.DataSource = table

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        table.Rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxLN.Text, TextBoxAGE.Text)

        DataGridView1.DataSource = table

    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        ' get the index of the selected datagridview row
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow

   ' show data from the selected row to textboxes
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
         ' the new row
        Dim newDataRow As DataGridViewRow

        newDataRow = DataGridView1.Rows(index)

   ' get data from textboxes to the row
        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

        DataGridView1.Rows.RemoveAt(index)

    End Sub
End Class

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




vb.net add a row to datagridview using textboxes
vb.net add a row to datagridview
vb.net update a datagridview row using textboxes
vb.net update a datagridview row

vb.net delete a datagridview selected row
vb.net delete a datagridview row





VB.NET - How To Delete A DataGridView Selected Row Using VB.NET

VB.NET - How To Remove DataGridView Selected Row In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Delete a DataGridView Selected Row In VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Delete_DataGridView_Selected_Row

' Create a new datatable
    Dim table As New DataTable("Table")

    Private Sub Delete_DataGridView_Selected_Row_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Add columns to your datatable, 
    ' with the name of the columns and their type 
        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

     ' Add rows to the datatable with some data

        table.Rows.Add(1, "XXXX", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        table.Rows.Add(7, "aefht", "cvfhytrff", 21)
        table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
        table.Rows.Add(9, "qsztii", "bvdhjh", 53)
        table.Rows.Add(10, "rytyufd", "esdfzr", 19)
   
   'set data from datatable to datagridview
        DataGridView1.DataSource = table

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

       ' get the index of the selected row
        Dim index As Integer
        index = DataGridView1.CurrentCell.RowIndex

        ' delete the selected row
        DataGridView1.Rows.RemoveAt(index)

    End Sub

End Class

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


vb.net delete datagridview row
Before Deleting

vb.net remove datagridview selected row
After Deleting





VB.NET - How To Update A DataGridView Row With TextBox Using VB.NET

VB.NET - How To Update DataGridView Selected Row With TextBoxes In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Update a DataGridView Selected Row Using TextBoxes In VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Update_DataGridView_Row_Using_TextBoxes

    ' Create a new datatable
    Dim table As New DataTable("Table")

    Dim index As Integer

    Private Sub Update_DataGridView_Row_Using_TextBoxes_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Add columns to your datatable, 
        ' with the name of the columns and their type 

        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

        ' Add rows to the datatable with some data

        table.Rows.Add(1, "XXXX", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        'set data from datatable to datagridview
        DataGridView1.DataSource = table

    End Sub
     ' datagridview Cell click event
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
       ' get the index of the selected datagridview row
        index = e.RowIndex

        Dim selectedRow As DataGridViewRow

        ' display data from datagridview selected row to textboxes

        selectedRow = DataGridView1.Rows(index)

        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        ' the new row
        Dim newDataRow As DataGridViewRow
        ' get data from textboxes to the row
        newDataRow = DataGridView1.Rows(index)
        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text

    End Sub

End Class

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

vb.net update datagridview row using textboxes
VB.NET Update Selected DataGridView Row






VB.NET - How To Get Selected Row Values From DataGridView Into TextBox In VB.NET

VB.NET - How To Display Data From The Selected DataGridView Row IntoTextBoxes Using VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Displaying The Selected DataGridView Row Values To TextBox Using VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Selected_Row_From_DataGridView_To_TextBoxes
   ' Create a new datatable
Dim table As New DataTable("Table") Private Sub Selected_Row_From_DataGridView_To_TextBoxes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Add columns to your datatable, 
        ' with the name of the columns and their type
table.Columns.Add("Id", Type.GetType("System.Int32")) table.Columns.Add("First Name", Type.GetType("System.String")) table.Columns.Add("Last Name", Type.GetType("System.String")) table.Columns.Add("Age", Type.GetType("System.Int32"))

        ' Add rows to the datatable with some data
table.Rows.Add(1, "XXXX", "YYYYY", 21) table.Rows.Add(2, "SSDD", "hGSQ", 33) table.Rows.Add(3, "fgfgd", "jgfdd", 53) table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19) table.Rows.Add(5, "hghfd", "ghjgdf", 36) table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)
        ' load datatable data to datagridview
        DataGridView1.DataSource = table

    End Sub
    ' datagridview cell click event
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim index As Integer

       ' get the index of the selected datagridview row
        index = e.RowIndex

        Dim selectedRow As DataGridViewRow
        selectedRow = DataGridView1.Rows(index)

        ' now show data from the selected row to textboxes
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

End Class

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

Display Selected Row Values From DataGridView Into TextBoxes In VB.NET
Selected Row From DataGridView To TextBoxes Using VB.NET