Replies To: JTable; automatically adjust column width

Re: JTable; automatically adjust column width

Posted 21 June 2007 - 07:23 AM

I don't think that it happens in Mr Excel at all, since the table has a fixed width, and the columns have to fit into it (you can manually stretch the table and the columns, but it's possible with Java, too)
Anyway, I am sure, that you can accomplish what you are referring to with a custom table model.More here.

Re: JTable; automatically adjust column width

Posted 21 June 2007 - 07:47 AM

I dont seem to get what i want. I want to double-click the column header boundary and automatically the width adjusts to the longest phrase/sentence/data item in that column. It surely happens with Ms Excel, i have seen that.
1lacca, i think your previous avatar was great, why did you change it?

Re: JTable; automatically adjust column width

Posted 21 June 2007 - 08:15 AM

Hm, I forgot about the doublecliclk magic
Anyway, I think with a custom tableheader it is possible, just iterate over the the cells in the column, and find the may width they require. I'm not sure what is the easiest way to find the space required by a cell, but probably with a custom table model where the cells are displayed by a component which doesn't wrap lines (like a correctly parametrized jlabel) it should be easy.
Anyway, I'm a bit out of Swing programming right now, so somebody who is dealing with things like this right now can probably give a much simpler solution.
I there is a good sample for the first few steps in the Sorting and Filtering section of the tutorial I've linked in my previous post (how to create a custom header).

My avatar was changed on a funky night for well desereved, but less wished reasons, however it might return someday...

Re: JTable; automatically adjust column width

Posted 21 June 2007 - 08:17 AM

how big of a table are you looking at? You could always reset the data to it and it should align itself as if it were new.
There is also the option of placing it in a JScrollPane, I believe you can force the size of cells dynamically by measuring the pixel width of them and since you have scrollbars it has an unending amount of room to grow.

Re: JTable; automatically adjust column width

Posted 22 June 2007 - 02:50 AM

My table is of course in a JScrollPane and so i have unlimited space to make the column width grow. I wouldn't want to resize all the columns though, but rather resize the particular column on which the user would have chosen, in this case by double-clicking the border of the header.
How would i do that?