Can I convert a java.util.list to a TableModel or JTable?

Hi,
   I've spent hours looking for a simple solution to this, but my brain is completely frazzled at the moment and I've admitted defeat and decided to ask for some help :)

  I've written an application using Netbeans 6.8.1 and after some investigation have decided to standard built in queries using createNamedQuery are not returning the results I want, so I've decided to try and make my own.

  All the SQL queries seem to work fine through createNativeQuery, and if I println the returned List I can see all the information is returned correctly.

  What I am really stumped on is how to convert the returned java.util.List results into the required String[][] array needed to display the results in a JTable.

  My existing code is basically;

    java.util.List tmp = entityManager.createNativeQuery(query).getResultList();

  and an example (working) SQL statement is;

SELECT delaccount,invaccount,manufacturer,manufacturer_name,mon01 as MON01,year01,year02 from APP.ANALSALESUM WHERE INVACCOUNT = -1 ORDER BY mon01 DESC

Some of the fields within the query vary depending upon radio buttons selected within the application.

 I have created some dummy code which populates my TableModel field and is correctly displayed in my JTable, the code for this is;

String[] columnNames = {"
DelAccount","InvAccount","Name","Value","Year","Last Year"
};

String[][] tmp_array = {
  { "one ", "two", "three", "four", "five", "six" },
  { "seven", "eight", "nine", "ten", "eleven", "twelve" }
} ;

and I've had this code working and create JTable with the required results.

Now, I am very new to Java, so I might well be mixing and matching the wrong Lists to TableModel commands, so if someone could point me in the right direction or provide code to convert the List to the required TableModel format it would save me a sleepless night from drinking yet more coffee!!!

Thanks
nevertonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You can just transfer the List into a Vector for creating a DefaultTableModel. I take it that List of List is returned?
0
objectsCommented:
>   What I am really stumped on is how to convert the returned java.util.List results into the required String[][] array needed to display the results in a JTable.

you wouldn't. Instead loop throught the list and use the addRow() method on each iteration to add a row

0
nevertonAuthor Commented:
Hi,
   I'm obviously being completely thick here, but I can't get either of the solutions to work.

   How am I supposed to use these solutions in the code?

   If I try the addRow function as either TableModel.addRow or tmpArray.addRow() the program won't compile, and netbeans complains in the IDE it doesnt like the addRow.

  I can convert the List to Vectors, but then I'm not sure what to do with it.

  I am really sorry for being thick, but its doing my head in :(

  Any chance someone can post some example code to help me out?

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nevertonAuthor Commented:
Hi,
  Ignore me, finally got it sorted ( I think!), I did the following;

       Vector columnNames = new Vector (Arrays.asList("DelAccount", "InvAccount", "Name", "Value","Year", "Last Year"));

        Vector v;
        v = new Vector(tmp);

        TableModel manufacturersTableModel = new DefaultTableModel(v, columnNames);
        manufacturersTable.setModel(manufacturersTableModel);

  Not sure if this is 'correct' Java code, but it seems to work for me :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.