JTable problems

Hello. I have a problem with displaying the data inside a JTable. I am currently working with a database, where i have 1 table called "Client". In my application, I also have a class called ClientDTO, which I use as a data transfer object, which has as attributes the corresponding columns from the Clients table in the DB (name, cnp, address,idcardnr). In ClientsDBO i have a method which reads entries from the database, converts them to ClientsDTO objects, and returns an ArrayList containing all these objects. The question is: How do i take the data from my array list and display them in a JTable? I also need to be able to specify the names of the columns from the JTable manually.

The usual way to do that is to create a TableModel from the data, to have a JTable in your GUI, and to call the JTable's setModel() method to assign that TableModel to it.

As for setting the column names manually -- which I assume must mean "at compile time" -- you use the TableColumnModel for that. There's a setColumnModel in JTable which you can call with your column model.

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90

posted Apr 02, 2012 10:56:51

0

Ok, thank you. I will try that.

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90

posted Apr 02, 2012 12:23:09

0

Isn't there any other way to do this? I don't want to create a new class for the table model.

That seems like a rather round-about and clumsy way of implementing the table model: you already decided to have an ArrayList of ClientDTO objects, so the natural approach would be to write a subclass of DefaultTableModel which could use one of those.

But if you want to go with the two-dimensional array approach, then the simplest way to implement that would be to build a two-dimensional array in the first place, instead of an ArrayList<ClientDTO>.

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90

posted Apr 02, 2012 13:04:43

0

I don't know how to build that. I tried declaring it like this: String data[][];
But then when i try to put information in it, it tells me that it hasn't been initialized. And I don't klnow how to initialize such a structure

I see. But if you haven't got to the point where you know how to create an array in your code, I think you're getting a bit ahead of yourself trying to write this application. So put it aside for a while and catch up on the basics. Here's a link to the Java tutorial about arrays: Arrays.

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90

posted Apr 02, 2012 13:27:17

0

Well, I solved the problem. Thank you very much for your help. I haven't used arrays very much, so that's why I don't know how to initialize one