| | |
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:
Solved Threads: 40
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.
•
•
Join Date: Mar 2004
Posts: 802
Reputation:
Solved Threads: 40
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.
Java Syntax (Toggle Plain Text)
JScrollPane libraryScrollPane = new JScrollPane(libraryTable); libraryScrollPane.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { Component c = e.getComponent(); int viewWidth = c.getWidth(); TableColumn tc = libraryTable.getColumnModel().getColumn(libraryTableModel.getColumnCount()-1); int tableWidth = libraryTable.getWidth() - tc.getWidth(); int diffWidth = viewWidth - tableWidth; if (diffWidth > 0) { tc.setPreferredWidth(diffWidth); } } });
Try this 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.
Java Syntax (Toggle Plain Text)
Component c = evt.getComponent(); int viewWidth = c.getWidth(); int tableWidth = libraryTable.getWidth(); int diffWidth = viewWidth - tableWidth; if (diffWidth > 0) { TableColumn tc = libraryTable.getColumnModel().getColumn(libraryTable.getColumnCount()-1); tc.setPreferredWidth(tc.getWidth() + diffWidth - 3); }
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.
•
•
Join Date: Mar 2004
Posts: 802
Reputation:
Solved Threads: 40
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?
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 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?
This change
Java Syntax (Toggle Plain Text)
if (diffWidth > 0) { int newWidth = tc.getWidth() + diffWidth - 3; tc.setPreferredWidth(newWidth); tc.setMinWidth(newWidth); }
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?
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Messenger Help
- Next Thread: about arguments at netbean5.0
Views: 5608 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android api apple applet application arguments array arrays automation binary block bluetooth chat class classes client code compile component database developmenthelp draw eclipse encode error event exception file fractal freeze game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working






