auto resize only 1 column in jtable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

auto resize only 1 column in jtable

 
0
  #1
Dec 23rd, 2007
Any direction someone could point me in for auto-sizing only the last (right-most) column in a jtable? I want the user to be able to set the size of columns and be allowed to set them beyond the scrollable viewing area, but if the viewing area is wider than the column widths, then the last column would be auto-sized to fit the empty space.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: auto resize only 1 column in jtable

 
0
  #2
Dec 24th, 2007
I figured it was something simple, just needed to pull my eyes from the screen for a bit. I made the right-most column an empty dummy column that basically just fills the empty space when a view is wider than the table being displayed.

  1. JScrollPane libraryScrollPane = new JScrollPane(libraryTable);
  2.  
  3. libraryScrollPane.addComponentListener(new ComponentAdapter()
  4. {
  5. public void componentResized(ComponentEvent e)
  6. {
  7. Component c = e.getComponent();
  8. int viewWidth = c.getWidth();
  9. TableColumn tc = libraryTable.getColumnModel().getColumn(libraryTableModel.getColumnCount()-1);
  10. int tableWidth = libraryTable.getWidth() - tc.getWidth();
  11. int diffWidth = viewWidth - tableWidth;
  12.  
  13. if (diffWidth > 0)
  14. {
  15. tc.setPreferredWidth(diffWidth);
  16. }
  17. }
  18. });
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: auto resize only 1 column in jtable

 
0
  #3
Dec 24th, 2007
I've tried almost the same code to control the dummy column's width when the user resizes a column, but the width of resizing column jumps all around the place.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: auto resize only 1 column in jtable

 
0
  #4
Dec 26th, 2007
Have you tried setAutoResizeMode(int) using AUTO_RESIZE_LAST_COLUMN ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: auto resize only 1 column in jtable

 
0
  #5
Dec 26th, 2007
I swear I looked at all the constants, now I feel like an idiot. Will that still allow the table width to be greater than the view?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: auto resize only 1 column in jtable

 
0
  #6
Dec 26th, 2007
No, sorry, it won't. I should have checked it before suggesting.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: auto resize only 1 column in jtable

 
0
  #7
Dec 26th, 2007
Try this
  1. Component c = evt.getComponent();
  2. int viewWidth = c.getWidth();
  3. int tableWidth = libraryTable.getWidth();
  4. int diffWidth = viewWidth - tableWidth;
  5.  
  6. if (diffWidth > 0) {
  7. TableColumn tc = libraryTable.getColumnModel().getColumn(libraryTable.getColumnCount()-1);
  8. tc.setPreferredWidth(tc.getWidth() + diffWidth - 3);
  9. }
The magic number 3 is just there to keep it from being just a tiny bit wider than the internal display area and forcing an unnecessary scroll bar (I didn't spend the time to find out which component width was causing it - probably just a border or inset on one of them).

That does still allow them to manually make the last column narrower than the scroll pane. If you really want to avoid that you would need to call this when a column is resized as well.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: auto resize only 1 column in jtable

 
0
  #8
Dec 26th, 2007
I have that part working ok, also noticed the issue which needed the 'magic number' as well. The problem I'm having is that for whatever reason, calling this when a column is manually resized doesn't always work. I'm not sure if it's because when I resize a column the method resizes the last column which in turn calls the listener again. Is there a way to detect which column was resized triggering the event?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: auto resize only 1 column in jtable

 
0
  #9
Dec 26th, 2007
Not from the ChangeEvent supplied by columnMarginChanged() unfortunately. In most case, the resize affects more than one column at the same time, so it's up to you to handle a particular column on your own. I fiddled with a few things but didn't come up with a clean way of avoiding that circular cascade in the resizing and still have it resize to fill the pane after they are done trying to make it smaller. That's to be expected though since they are actually fighting against the listeners with the mouse to drag it smaller and the code is just trying to respond the way you want it to.

This change
  1. if (diffWidth > 0) {
  2. int newWidth = tc.getWidth() + diffWidth - 3;
  3. tc.setPreferredWidth(newWidth);
  4. tc.setMinWidth(newWidth);
  5. }
does prevent them from making it too small to fill the pane, but if the min size gets larger from a panel resize, it can't be set back to a smaller size later. It becomes a one way street in that respect.

A relevant question might be though, if the user intentionally makes that column narrower and it doesn't fill the pane, should you care enough to prevent it?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: auto resize only 1 column in jtable

 
0
  #10
Dec 27th, 2007
not really, but its nice to keep the look clean and uniform.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 5608 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC