JTable with Arrays

I am after Jtable code that will display the rows and columns of a 2d array called returned array. Can anyone help? I am currently using a for loop within main to populate a variable with the values from the array, which is then displayed via a dialog box.

Cheers

CyartAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
Vector data = new Vector();
Vector cols = new Vector();
// add columns names

for (int i=0; i<array.length; i++)
{
   Vector row = new Vector();
   for (int j=0; j<array[i].length; j++)
   {
      row.add(new Integer(array[i][j]);
   }
   data.add(row);
}
JTable table = new JTable(data, cols);
0
 
CyartAuthor Commented:
can you explain what a vector is cheers?
0
 
zzynxSoftware engineerCommented:
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
A Vector class is a (growable) array of objects.
0
 
CyartAuthor Commented:
Ok how do I display the table
0
 
objectsCommented:
add it to your component hierarchy.
eg. to add it to your dialog you could use:

dialog.getContentPane().add(table);
0
 
NaeemgCommented:
use JScrollPane and add ur JTable to it

    jspTableScroller.getViewport().add(jtData, null);


Naeem Shehzad Ghuman
0
 
suprapto45Commented:
I will post a code soon.
0
 
scanadminCommented:
create a model class extending AbstractTableModel,

place your vectors in there and fill in the code for the getElementAt(int row, int col)

then make your jtable and pass it the new class as its model,

when you call fireStateDataChanged(); in the model this will fill the data for you.
0
All Courses

From novice to tech pro — start learning today.