How to add dynamically rows number in jTable - Java

Most of the programming now a days required dynamically create controller such as button, textbox and also table. Today i want to show how to dynamically set rows number in jTable Swing Controls.

Firstly is to set the DefaultTableModel and assign to jTable.

Here is the example :

The DefaultTableModel

import javax.swing.table.DefaultTableModel; 

:
:

String colNames[] = {"Name", "Edit", "View"}; 
DefaultTableModel dtm = new DefaultTableModel(null,colNames); 



Do looping to add rows in jTable

for(int i=0; i < listOfString .size();i++){
            dtm.addRow(new String[3]);

}




Assign to jTable

 jTable2.setModel(dtm);



The full Code :

 String colNames[] = {"Name", "Edit", "View"}; 

 DefaultTableModel dtm = new DefaultTableModel(null,colNames); 

List<String> listOfString = new ArrayList<>();
        listOfString.add("1");
        listOfString.add("2");
        listOfString.add("3");
        listOfString.add("4");
        listOfString.add("5");
        listOfString.add("6");
        listOfString.add("7");

       
 for(int i=0; i < listOfString .size();i++){
       dtm.addRow(new String[3]);
}
       
 jTable2.setModel(dtm);




By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico