• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying JTable Column names in the GUI

 
Charu Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charu - I used the array to, and I got a 20/20 on GUI.
Kalichar - I just enlarged the JFrame
Mark
 
Thomas Smets
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I thought MultilineHeaderRenderer was in the standard API. You can find the code for it at
web page
regards,
Henk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic