Thursday, January 26, 2012

the fun of Swing JTable column resizing

Tagged with: , ,
Saturday, August 30, 2008, 16:17
This news item was posted in Java category and has 3 Comments so far.

The JTable is one of Swing most powerful component, it is quite flexible, scales very well with large models, allows full customization of the rendering and editing, most recent version added easy printing, sorting, filtering… but there is one thing that I never liked in the JTable, the way columns get resized.

JTable has 5 resize policies, I do not like any of them, the one I like does not exist, but is the one you find in Windows Explorer. Let see how they compare:

 

As I prefer the Windows Explorer behavior and look, I tried to replicate it. This leads to this result:JTableResizePolicy.jpg

See how the table look after the last column, it’s like the table header has been painted after the table. It’s almost the case. In fact there is another table header there to paint this last filler column. And here is the source code for 6th resize policy, the TableHeaderFiller, I let the reader understand it:

TableHeaderFiller.java

To use it, just call new TableHeaderFiller(table), make sure the table resize mode is AUTO_RESIZE_OFF and make sure the table has already been added to its scrollpane.

You can leave a response, or trackback from your own site.

3 Responses to “the fun of Swing JTable column resizing”

  1. Jeanette Winzenburg said on Tuesday, September 9, 2008, 15:54

    Hi Fred,

    cool idea to add a dummy header :-) Couldn’t resist playing a bit (some code in my swingx incubator, table package under java and test source hierarchies) and having a couple of comments (in no particular order)

    - to my surprise it’s working cool with SwingX JXTable and its support for hiding columns.
    - the filler isn’t updating its property listeners to the tableColumns on add/remove to/from the columnModel (which probably is the reason its working with JXTable). In the case of a remove that’ll leave dangling listeners in the filler. Looks like you don’t really need them: the filler is useful only if AUTO_RESIZE_OFF which will always resize the table, so the notification reaches your filler via the componentEvent. Maybe you should listen to mode changes and un-/install the filler as appropriate
    - the filler can’t cope with setting a new tableColumnModel - not the most probable event, but just for completeness :-)
    - the visual appearance isn’t very appealing in LAFs where the viewport background is different from the table background.
    - the functionality is not quite the same as in explorer: f.i. a right click on the area below the filler should show the table’s popup (as far as I remember Santhosh has an example of how to re-target a mouselistener)
    - the filler gets a bit confused with with LAF updates (again not the most frequent requirement, but …): to reproduce in my example resize the last column with the mouse, toggle the LAF and move the mouse over the filler - the cursor now is always a resize cursor. The most probable reason (didn’t dig, though) might be the mouselistener installed by the new LAF, which are not removed (as you do when creating the filler)

    Good work!

    Jeanette

  2. Swing links of the week: September 7 : Pushing Pixels said on Tuesday, September 9, 2008, 19:01

    [...] Lavigne adds a JTable feature that he is missing from the Windows Explorer application - table header filler that spans to fit the available width [...]

  3. kaprasanna said on Friday, February 19, 2010, 13:01

    Hi,
    I know this isn’t the right place to ask for technical help but I didn’t find any other forum / newsgroup.
    Hence the comment come seeking tech help about l2fprod property sheet.
    We want to fire property change event the moment user starts typing some value for a property.
    Have raised a thread in nabble : http://bit.ly/9xoqHi
    Please reply.
    Thanks.

Leave a Reply