This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JTable problems

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thank you. I will try that.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't there any other way to do this? I don't want to create a new class for the table model.
 
Paul Clapham
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Why on earth not?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a very small project, for school, and i already have too many classes. I will try another approach then.
 
Paul Clapham
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic