wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Interchanging Database columns as rows while displaying in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Interchanging Database columns as rows while displaying in a JTable" Watch "Interchanging Database columns as rows while displaying in a JTable" New topic
Author

Interchanging Database columns as rows while displaying in a JTable

sum prit
Greenhorn

Joined: Mar 13, 2011
Posts: 14
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

Joined: Jul 26, 2007
Posts: 9995
    
    7

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
sum prit
Greenhorn

Joined: Mar 13, 2011
Posts: 14
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

Joined: Jul 26, 2007
Posts: 9995
    
    7

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

Joined: Mar 13, 2011
Posts: 14
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

Joined: Jul 26, 2007
Posts: 9995
    
    7

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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7



Thanks sum prit for wasting my time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interchanging Database columns as rows while displaying in a JTable
 
Similar Threads
JTable problem
How to select a row in a Jtable?
Problem with JTable
Jtable question
Problem with JTable