• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help Need for JTable / Object[][] array

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sridhar Ranganathan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic