• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Adding 2-d ArrayList to JTable

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I've spent 4 hours trying to read the data from file to ArrayList and then to JTable, but still cannot solve one problem.

I have the following class UserDataRow, where the data about a user is described:



I'm using the following code to read the data from file:



Now I want just to put this 2d array "listofusers" into my JTable, but I cannot... The above code does't work.



The error message is:


So, where could be my error?
 
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection.toArray() returns an Object[]. Even though its elements may be Object[] themselves, the array is still an Object[], not an Object[][]. Try the following:
Note that you no longer need to cast. Check out the Javadoc of Collection.toArray(T[]) for more information.
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, that's a bit hard to read. It's saying you are casting a two-dimensional array to a one-dimensional one. Like Object[][] to Object[]. I didn't read through all your code, but try to look for something like that.

ETA: Oops, beaten by 20 seconds and a better answer!
 
Liana Norpunova
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I've tried Object d1[][] = filetree.Form.listofusers.toArray(new Object[0][]); Now I get only one error message:

Error: 6

What does it mean?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you aware that this:
Does the same as:
I don't know what is in the file but are you sure you want to do this:
and not this:
It just looks weird to me.
 
Liana Norpunova
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Now I use one-dimensional array. There are no errors in the code. But the table is empty. Any advises pliz?



 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While you have a one-dimensional Object[] array present, each item in this array is a UserDataRow object, and you are adding these items as rows to the table model. I don't know how the DefaultTableModel will be able to interpret that since it only knows how to handle Object arrays or Vectors as rows. One solution: create an Object[] array within the for loop to use as a row and add it to the model, or 2: create and use an AbstractTableModel that knows how to handle UserDataRow objects (this way is harder).

To clarify, this:


adds an Object array as a row to the table model, but this Object array has one and only one element, a UserDataRow object. Better would be to do something like so (semi-pseudo-code):

>
 
reply
    Bookmark Topic Watch Topic
  • New Topic