Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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

0
Cyart
Asked:
Cyart
1 Solution
 
objectsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now