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

   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!!!

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

You can just transfer the List into a Vector for creating a DefaultTableModel. I take it that List of List is returned?
>   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

nevertonAuthor Commented:
   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?

nevertonAuthor Commented:
  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);

  Not sure if this is 'correct' Java code, but it seems to work for me :)
All Courses

From novice to tech pro — start learning today.