Java Basic Problems

Java:How to add data to JTable by declaring and using DefaultTableModel.

If you want to add data in JTable by using DefaultTableModel then this solution will be helpful for you. But,if you don’t want to use DefaultTablelModel then CLICK HERE.

Code Example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class SimpleTableTest extends JFrame


private JPanel topPanel ;

private JTable table;

private JScrollPane scrollPane;

private String[] columnNames= new String[3];

private String[][] dataValues=new String[3][3] ;

public SimpleTableTest()


setTitle(“JTable and DefaultTableModel”);


topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());



columnNames=new String[] {“Column 1” , “Column 2” , “Column 3”};

 dataValues = new String[][]   {





  TableModel model=new myTableModel(“owntable”);

  table =new JTable( );



  scrollPane=new JScrollPane(table);



            public class myTableModel extends DefaultTableModel


                                                                                     myTableModel(String tname)



                                                                                       System.out.println(“Inside myTableModel”);


                                                                                    public boolean isCellEditable(int row,int cols)


                                                                                                  return true;



            public static void main(String args[])


            SimpleTableTest mainFrame=new SimpleTableTest();






JTable using DefaultTableModel

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s