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?

Improve company productivity with a Business Account.Sign Up

x
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.