• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

NX: Setting JTable column size

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can someone give me some advice. I am trying to set the size of my columns that is displaying the contractors. I have the following code.

The table has been placed within the scrollPane and then added to the Frame using a BorderLayout at the Center.
My problem is all the columns are being displayed with the same column width.
The value being set as the preferred width is correct, so it leads me to think it as a LayoutManager issue. So I tried to add the table to a JPanel that uses FlowLayout but that did not help. So then I tried to add the table to a JPanel that uese GridLayout(1,0) but that did not help.
Thanks
Chris
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
Figuring out how to get column widths right is one of the more challenging aspects of dealing with JTable for the uninitiated. I don't want to spoil all your fun by telling you how to do it , but I will give you a hint:
Consider the effects of JTable's "autoResizeMode" property.
kktec
SCJP, SCWCD, SCJD
"The journey is the reward."
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken,
Thanks for the hint. I added the line

before my loop, but still not joy . So I then tried to also add

After the loop but still NO.
If I do


All the columns are look like they have the width of 1. Which could make it a little hard to read

I am now running out of ideas. Can anyone point out what I am doing wrong.

Thanks
Chris
[ November 22, 2003: Message edited by: Chris Harris ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone point out what I am doing wrong.
For each of the methods you have which deal with length/width of something, check the documentation to see if they're talking about the number of characters, or number of pixels.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
below you can find my (preliminary) version that deals with your problem.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chris, did you find your solution yet?
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet
I have been following sun's example and have ended up with the following code:

It is close. but there are a couple of colums that a just a little to small.
I am trying to find a solution that I understand and don't just want to drop on in that would work.

Chris.
[ November 24, 2003: Message edited by: Chris Harris ]
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
I don't know specifically what your problem is but here's an ideas you can look at that might point you in the right direction.
To get the column sizing behavior (AUTO_RESIZE_NEXT_COLUMN) I wanted, here's what I did to size the table:

Read the Javadoc on JTable.doLayout to find out why .
kktec
SCJP, SCWCD, SCJD
"The journey is the reward."
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never once came across a problem and my code is very simple.
I set up the column widths

Then I add the table panel to a JScrollPanel and and the tableScroll
to a Panel that has a border layout. A add it to the center.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't finished, I dont know what happened. I will try again.
First I create the table and size the columns:

Anyway after sizing the columns in the table I add the table to the scrollpanel and the scrollpanel to a borderlayout panel. thats it.

By the way mainTable = new JTable() and with no settings. The only thing
I set is mainTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
But that should be irrelevant. Other than that mainTable is just a plain old JTable with all the regular default settings.
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your advice.
I have done it.
THANKS
Chris
 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boy, I'm having the same problem as Chris was. I'm trying to set things up similar to Bill's example above with the separate method to set the table column sizes. Here is my code:

I have also looked at Ken's example above and am having trouble understanding why you have to do the resizing with RESIZE_NEXT_COLUMN and also why you have to turn it on, off and then back on.
Like Chris mentioned above, I also want to really understand how this works rather than just dropping in some code that seems to work.
Any ideas on what I'm doing wrong???
Any help appreciated.
TJ
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terry,
I wish I could give some great insight on how it works but
to be honest with you all I am doing is:
1. Getting a handle to my main table
2. Using my handle to return my columns
3. Then sizing my columns
4. And then finally adding my table to a scrollpanel and the scrollpanel
to a JPanel. Then my JPanel eventually is added to my main JFrame.
I guess you could say I got lucky in that this is just they way I
picked how to do it from the start if you know what I mean.
I can't tell you if adding the table to the scrollPane and then the scrollPane to your interface before you do steps 1 through 4 (which you is what are doing by the way) has any effect on the outcome .
For the hell of it maybe try doing it step by step like my example
and then if it works start picking it apart to see what makes the difference. Anyway good luck.
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill - I will try that.
One other question - did you ever do the "doLayout()" method in yours?
Thanks.
TJ
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I though I would say how I finally got it to work.
Using my the last code post I added the line at the end of my setuptable method:
table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
I do not need to call doLayout or setAutoResizeMode before the changing the column width.
Hope that helps
Chris.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


One other question - did you ever do the "doLayout()" method in yours?


nope. never even considered it.
I would like to know what was the common "theme" that caused so much
problems by everyone.
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
My problem can from attempting to dynamically change the width of the columns.
Thanks to everyone here, my problem has now been resolved . This means I am coming to the end of the GUI section of the assigment.
Chris
 
Terry Martinson
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code seems to be working now. I think I can chalk my problem up to an ID 10 T problem. Anyone ever heard of that? I think my table was adjusting the sizes correctly in my original version, but they were all so very close (because of how the doLayout() is distributing the extra column width) that I didn't notice the difference.
I ended up with something similar to Bill's example above with the exception that I use AUTO_RESIZE_ALL_COLUMNS and I use the doLayout() method. This deals with resizing columns and distributing the delta across all columns which I currently think I need (even though removing the doLayout() doesn't change much in my display currently -- I'm leaving it in case there is some scenario that I don't know about that would require the doLayout to properly display stuff). I did add a JPanel to hold the JScrollPane since I posted my original code attempt.
I did not do any of the fancy stuff like Chris with the TableCellRenderer or cell width/header width. I hope the approach I use is not simplified TOO much. Being a GUI newbie, I'm trying to keep things as simple as I can.
Thanks all for the help!
TJ
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there was any part of the assignment that I spent way too much time on (besides the locking/threading issues) it was the GUI, particularly the table rendering. I suppose it's because I've worked with so many badly designed UI's that I think it's worth taking the time to make things simple and pleasant for the user. BTW, my method described above does support dynamic column sizing.
kktec
SCJP, SCWCD, SCJD
"The journey is the reward."
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic