Sunday, 3 November 2013

How to add dynamically rows number in jTable - Java

Leave a Comment
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.