File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Displaying JTable Column names in the GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Displaying JTable Column names in the GUI" Watch "Displaying JTable Column names in the GUI" New topic
Author

Displaying JTable Column names in the GUI

Charu Krishnan
Greenhorn

Joined: Oct 24, 2000
Posts: 19
I am trying to display Column Names as headers in the JTable. I am able to display the default column names. But for clarity would like to change the name displayed in the GUI eg Filght#, Origin, Destination etc. I understand that one must use renderers, but am unable to get it to work. Is there anyone who could tell me how they did this or are there any usefull links you know to help one do so.
Thanks
Charu
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3286
    
    7
You don't have to use renderers for this. All you have to do is have the method getColumnName(int column ) of AbstractTableModel return the name that you want to be displayed for that column (ie. the column parameter).
You can keep an array of strings (column names) in your impl. of ATM and then do : return names[column]; in this method.
-Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
If you want your code to be generic and the results to be dynamically decided then you have to use the column names from the database itself.
if the idea is to shorten the names, I can't think of any way to automatically reduce the size and yet be meaningful
Charu Krishnan
Greenhorn

Joined: Oct 24, 2000
Posts: 19
Thank you.
I used a predifined array of column names and returned these values from getColumnName method and it worked. Thanks. I do understand though that this would take away from the model being generic. Is taht a problem? . Can anyone who completed the assignment and got graded let me know if they were penalized for this?.
Thanks
Charu
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
Does anyone know of an easy way to set the column widhts of a JTable so that all columns display correctly? I would like to do so without increasing the frame size. Thanks


Never be satisfied with anything less than the best and you will surely pass the test...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Charu - I used the array to, and I got a 20/20 on GUI.
Kalichar - I just enlarged the JFrame
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Thomas Smets
Ranch Hand

Joined: Dec 11, 2001
Posts: 111
May be it would be nice put indicate what you've tried & did not suite U !
I would sugget you to drop first your JTable on a JScrollPane .
Firstly,
Doesn't void sizeColumnsToFit ( int resizingColumn) fit your needs ?
Secondly,
I would play (extend) with the DefaultTableColumnModel() to set the width via access to the javax.swing.table.TableColumn.

Enumeration enum = myJTable.getColumns();
TableColumn tc = null;
while (enum.hasMoreElement())
{
tc = (TableColumn) enum.nexElement();
// dowhatYouWant
tc....
}

[ January 29, 2002: Message edited by: Thomas SMETS ]

Thomas Smets
Just another developper
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
My problem is that the gui is a bit long as of now. It seems to be approx 1024x650 if I have to make all the columns viewable and in general looks bad . I would like to set the widths of the columns so that some columns like price etc dont take up that much space. Not really sure how to get this to work.
Kalichar Rangantittu
Ranch Hand

Joined: Jan 15, 2002
Posts: 240
I have my table withing a SCroll pane alright. Its just that the columnn sizes for some of the fields take up too much space when not required. I am wondering if there is a way to set the width as we have the supposed width of the column by, ie, Destination Airport 25 characters etc.
Can we calculate the min width from this?
HenkGijsbert
Greenhorn

Joined: Jan 07, 2002
Posts: 28
Hi Kalichar,
Would not using multi line headers help you:
MultiLineHeaderRenderer renderer = new MultiLineHeaderRenderer();
Enumeration enum = table.getColumnModel().getColumns();
while (enum.hasMoreElements()) {
((TableColumn)enum.nextElement()).setHeaderRenderer(renderer);
}
regards,
Henk van Jaarsveld
Reshma Das
Ranch Hand

Joined: Jan 29, 2002
Posts: 125
what is the package structure for 'MultiLineHeaderRenderer'. i was not able to locate it in api.
how exactly does it work?
Reshma Das
Ranch Hand

Joined: Jan 29, 2002
Posts: 125
i found the code for multilinerenderer:
........
dm.setDataVector(new Object[][]
{{"a","b","c"}, {"A","B","C"}},
new Object[]{"1st\nalpha","2nd\nbeta","3rd\ngamma"});
the above code has "\n" i think this makes the header to appear in 2 lines. in our case it is space delimited.
is there any way i can trap the price, day and shrink there column width?
thanks,
reshma
HenkGijsbert
Greenhorn

Joined: Jan 07, 2002
Posts: 28
Sorry I thought MultilineHeaderRenderer was in the standard API. You can find the code for it at
web page
regards,
Henk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying JTable Column names in the GUI