Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interchanging Database columns as rows while displaying in a JTable

 
sum prit
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....., everyone. I need to display database values in a JTable but in a different way. My database table has 2 rows and 3 columns. Is it possible to make 3 rows and 2 columns and display.... I mean to say that is it possible to change columns as rows?? I am using java swing.

Can anyone give me some suggestion. I cannot find any solution. Please provide me some useful solution so that i can continue with my work.

Thank you in advance.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JTable doesn't know nor care what the data it displays.
It just queries the underlying model to get the data to display.

You can create and provide such a model to the table which will switch the row-column data.
More info on tables here http://download.oracle.com/javase/tutorial/uiswing/components/table.html
 
sum prit
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sir for your reply. But sir can you give me a more useful suggestion regarding this. Actually i have gone through the given link. It displays hardcoded data, but i need the datas from database and also in different way i.e columns as rows. i am trying but still found myself in the same position. i cannot step forward, please give me more suggestion.

Thank you.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code:

1) Make call to DB
2) Prepare a model from this data (hint constructor)
3) Pass this model to the table (hint JTable#setModel())
 
sum prit
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply and suggestion. I am following this rule only. But i cannot able to prepare a model for the table. can you help me out of it. i am stuck at this point, after that i think i can do it.

Thank you sir.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In psuedo code

1) Connect to DB
2) Form and execute queries
3) Iterate over ResultSet.
4) Create a collection from the ResultSet values (what collection? what kind of objects inside? Depends on your requirement)
5) Pass the collection to the model constructor

In the model:
Imagine a List of Objects is what you created (#4 above)
1) THe model needs to tell how many rows. This would be the number of objects in your List
2) THe model needs to tell how many columns. This would be the number of attributes per object in your collection.
3) The model needs to tell what value per cell. This would be the object at the row index and the attribute at the column index.

Forget the DB part for now. First think and decide what kind of collection you need to have for your application. What kind of objects should go inside it.
Create a dummy collection and try to get it working first. Once you do that, linking to the DB is just a method call away.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sum prit, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/awt-swing/41131-displaying-database-values-row-jtable-but-not-column.html

I also found two earlier cross posts.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:sum prit, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/awt-swing/41131-displaying-database-values-row-jtable-but-not-column.html
I also found two earlier cross posts.


Thanks sum prit for wasting my time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic