[java] How to add row in JTable?

Do you know how I can add a new row to a jTable?

This question is related to java swing jtable

The answer is


To add row to JTable, one of the ways is:

1) Create table using DefaultTableModel:

        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Code");
        model.addColumn("Name");
        model.addColumn("Quantity");
        model.addColumn("Unit Price");
        model.addColumn("Price");
        JTable table = new JTable(model);

2) To add row:

        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 4","Column 5"});

Use

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

    Vector row = new Vector();
    row.add("Enter data to column 1");
    row.add("Enter data to column 2");
    row.add("Enter data to column 3");
    model.addRow(row);

get the model with DefaultTableModel modelName = (DefaultTableModel) JTabelName.getModel();

Create a Vector with Vector vectorName = new Vector();

add so many row.add as comumns

add soon just add it with modelName.addRow(Vector name);


Use:

DefaultTableModel model = new DefaultTableModel(); 
JTable table = new JTable(model); 

// Create a couple of columns 
model.addColumn("Col1"); 
model.addColumn("Col2"); 

// Append a row 
model.addRow(new Object[]{"v1", "v2"});

For the sake of completeness, first make sure you have the correct import so you can use the addRow function:

import javax.swing.table.*;

Assuming your jTable is already created, you can proceed and create your own add row method which will accept the parameters that you need:

public void yourAddRow(String str1, String str2, String str3){
  DefaultTableModel yourModel = (DefaultTableModel) yourJTable.getModel();
  yourModel.addRow(new Object[]{str1, str2, str3});
}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jtable

Java JTable getting the data of the selected row How to add row of data to Jtable from values received from jtextfield and comboboxes Load arrayList data into JTable Display calendar to pick a date in java Most simple code to populate JTable from ResultSet JTable - Selected Row click event Programmatically select a row in JTable How to add checkboxes to JTABLE swing Change the background color of a row in a JTable How to add row in JTable?