aspose file tools*
The moose likes Beginning Java and the fly likes JTable problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTable problems" Watch "JTable problems" New topic
Author

JTable problems

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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
Ok, thank you. I will try that.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
Isn't there any other way to do this? I don't want to create a new class for the table model.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Rus Corina wrote:I don't want to create a new class for the table model.


Why on earth not?
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
It is a very small project, for school, and i already have too many classes. I will try another approach then.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Sorry, you didn't mention in your original post that there was a limit on the number of classes you were allowed to create.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
I was actually hoping to be able to do sonething like this:

String data[][] = {{"Vinod","100"},{"Raju","200"},{"Ranju","300"}};
String col[] = {"Name","code"};
DefaultTableModel model = new DefaultTableModel(data,col);
JTable table = new JTable(model);


But i don't know how to initialize a matrix of strings (String data[][]=???)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable problems