JAVA : Creating data table in java

24 Feb

Since few week I am trying to work in java. It was always my dream to work in java when I
was university student and was doing my masters.
I was keen to complete my final year project in java but time didn’t allow us to get our
dreams true.
After working in php for more than year, I feel it would be worthy working in java, so
I m here to discuss one of its cool feature.

Creating data table in java is simple.

you will need to create an object of JTable, give it data and column names and that’s it.
let’s see how our code look like.

String[] columnNames = {“first name”,”last name”,”sports”};
Object[][] data = {{‘faheem’,”Abbas”,”Cricket”},{“shazil”,”Hassan”,”Soccer”}};

and now

JTable table = new JTable(data,columnNames);
getContentPane.add(JScrollPane(table));

it you want to have a table with some extra functionalities, you will need to create your our
own table model by extending it from AbstractTableModel and override some of the methods
provided by this abstract class.

I am giving its example as

class MyTableModel extends AbstractTableModel
{

String[] columnNames = {“first name”,”last name”,”sports”};

Object[][] data = {{‘faheem’,”Abbas”,”Cricket”},{“shazil”,”Hassan”,”Soccer”}};

public int getColumnCount()
{

return columnNames.length;

}

public int getRowCount()

{

return data.length;

}

public String getColumnName(int col)
{

return columnNames[col];

}

public Object getValueAt(int row, int col)
{

return data[row][col];

}

public boolean isCellEditable()

{

return true/false;

}

}

Now while creating object of JTable, write

JTable table = new JTable(new MyTableModel());

7 Responses to “JAVA : Creating data table in java”

  1. Asim Zeeshan February 26, 2009 at 2:50 pm #

    I had no idea you were into Java too, nice article. thanks for sharing

  2. Mihir March 13, 2009 at 10:57 am #

    Hello,
    I want to bring data from MySQL database table into JTable.
    Can you please tell me how?

  3. Jonathan April 29, 2009 at 7:20 am #

    You might like GWT .

  4. Franco November 17, 2009 at 12:44 pm #

    with C# it’s easy to create a data table,

    DataTable dt = new DataTable();

    dt.TableName = “easy”;

    DataRow row;

    DataColumn cl = new DataColumn();

    cl.DataType = System.Type.GetType(“System.String”);

    dt.Columns.Add(cl);

    row = dt.NewRow;

    dt.Rows.Add(row);

    dt.Rows[0][0] = “ciao”;

  5. John January 26, 2010 at 3:49 am #

    I have to agree with Franco, C#’s is an extremely handy class. I wish Java had something like it.

  6. Eli March 20, 2010 at 1:32 pm #

    @John, @Franco: perhaps this is true, but this was an article about creating a datatable with Java, not C#.

  7. ibrahim December 31, 2010 at 12:27 pm #

    no Franco, with C# it’s too easy to create a table or dataset:

    DataTable dt = new DataTable(“MyTable”)
    .Columns.Add(“FirstName”, typeof(string)).Table
    .Columns.Add(“LastName”, typeof(string)).Table
    .Columns.Add(“Sports”, typeof(string)).Table
    .Rows.Add(“Lionel”, “Messi”, “Soccer”).Table
    .Rows.Add(“Michael”, “Jordan”, “Basketball”).Table
    .Rows.Add(“Maria”, “Sharapova”, “Tennis”).Table;

    DataSet ds = new DataSet(“MyDataSet”)
    .Tables.Add(“MyTable1”)
    .Columns.Add(“id”, typeof(int)).Table
    .Columns.Add(“name”, typeof(string)).Table
    .Columns.Add(“date”, typeof(DateTime)).Table
    .Rows.Add(1, “xm100”, DateTime.Today).Table
    .Rows.Add(2, “coup”, new DateTime(2010, 12, 31)).Table
    .Rows.Add(3, “dqwdwd”, new DateTime(2010, 6, 12)).Table.DataSet
    .Tables.Add(“MyTable2”)
    .Columns.Add(“FirstName”, typeof(string)).Table
    .Columns.Add(“LastName”, typeof(string)).Table
    .Columns.Add(“Sports”, typeof(string)).Table
    .Rows.Add(“Lionel”, “Messi”, “Soccer”).Table
    .Rows.Add(“Michael”, “Jordan”, “Basketball”).Table
    .Rows.Add(“Maria”, “Sharapova”, “Tennis”).Table.DataSet;

Leave a comment