aspose file tools*
The moose likes Beginning Java and the fly likes Help Need for JTable / Object[][] array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Need for JTable / Object[][] array" Watch "Help Need for JTable / Object[][] array" New topic
Author

Help Need for JTable / Object[][] array

sridhar Ranganathan
Greenhorn

Joined: Feb 06, 2008
Posts: 29
Hi,,

Object headers[] = { "IP", "Status" };
Object row[][]={{"1","abc"},{"2","cde"}};
JTable table = new JTable(row, headers);
Here I ,iam having arraylist value need to store in Object row,I tried with for loop,Its not working
For ex--->

for(int a=0;a<ar.size();a++)
{
Object row[][]={{a.get(a),"ON"}};
JTable table = new JTable(row, headers);
}

But in JTable iam getting first row only...could you please help me.
The way which i tried is correct ?

Thanks,
Sridhar.R
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
You're creating a new JTable on each loop. Bring the JTable inizialization outside the for loop. Also, you have to adjust the code to correctly put objects from arrayList to the object array.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

U should be getting only One row. I am not sure if its the first row. The problem is tat in each iteration of the loop a new row object is created with only one int value and "ON" value. The object crated in the earlier iteration is lost. And at the end of the iteration the last value in the ArrayList is added to the table and a new table is created. Hence u'r table has only one row

A 2-d Array is an array or arrays.
U can try this


Mohamed Sanaulla | My Blog
sridhar Ranganathan
Greenhorn

Joined: Feb 06, 2008
Posts: 29
Originally posted by Matteo Di Furia:
You're creating a new JTable on each loop. Bring the JTable inizialization outside the for loop. Also, you have to adjust the code to correctly put objects from arrayList to the object array.


I didnt clearly about yours "put objects from arrayList to the object array"


You mean say ...
JTable table =new JTable(row, headers);
for(int a=0;a<ar.size();a++)
{
Object row[][]={{ ar.get(a), "ON"},
};

The above code will get error right ?
Thanks,
Sridhar.R
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You mean say ...
JTable table =new JTable(row, headers);
for(int a=0;a<ar.size();a++)
{
Object row[][]={{ ar.get(a), "ON"},
};

The above code will get error right ?


Yeah it will generate error as row is declared after it is used which is illegal.

Get the JTable table = new JTable(row,headers) outside after the loop. Also you need to change the statement within the loop.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

This is more a beginner's problem. I'm not moving this to Swing / AWT since it the problem is essentially not related to JTable itself. The entire way of thinking is what's causing this problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Rob is right. And this whole thread is really confused and I can't understand what is going on.
Please make sure to maintain indentation and put code tags round the code. Please avoid writing U'r for You're; a lot of people find that difficult to understand.

Mohammed Sanaullah is quite right; what people call a 2D array is in fact an array of arrays. In the case of a table it is in fact an array of rows; each row contains a certain number of items, so you want the length of each smaller array to be the same, what is called a rectangular array. A bit like this
row0:0 1 2 3
row1:0 1 2 3
row2:0 1 2 3
Not like this
row0:0
row1:0 1 2 3
row2:0 1 2 3
row3:0 1 2
which is called a jagged array.

You are trying to get a table which looks like thisTo get that, you are trying to get an Object[][] into it. Now you want to see how you get an Object[][] which can be printed out like thisThat is in fact the same as I posted before, with bits deleted. Now try creating an Object[][] which prints out like that, and print it with for loops, preferably nested and using the length attribute. Forget about the +------------+ bits, just print out the number and name in rows.
Once you have got that working, you know you have an array which you can pass to the JTable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Need for JTable / Object[][] array