Java: How to add data to JTable using the easiest way.

Here I have shared with you the basic procedure to add data in a JTable  without declaring the DefaultTableModel.

Code Example:

import java.awt.*;

import javax.swing.*;

class simpletable 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 simpletable()

    {

setTitle(“Simple Table Application”);

setSize(300,300);

 topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

setDefaultCloseOperation(EXIT_ON_CLOSE);

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

dataValues = new String[][]     {

                                                                        {“1″,”2″,”3”},

                                                                        {“4″,”5″,”6”},

                                                                        {“7″,”8″,”9”}

                                                                    };

    table =new JTable(dataValues,columnNames );

    table.setRowHeight(50);

    scrollPane=new JScrollPane(table);

    topPanel.add(scrollPane,BorderLayout.CENTER); 

            }

            public static void main(String args[])

            {

            simpletable mainFrame=new simpletable();

            mainFrame.setVisible(true);

            }         

             }        

OUTPUT:

add data in jTable

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s