JTable; automatically adjust column width

adjust column width by double-clicking header border

Page 1 of 1

5 Replies - 13835 Views - Last Post: 22 June 2007 - 02:50 AM Rate Topic: -----

#1 gyron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 09-January 07

JTable; automatically adjust column width

Posted 21 June 2007 - 06:57 AM

How do i automatically adjust the column widths to accommodate the longest data item in the cells under the column header? This happens with Ms Excel, or even a listview in VB.
Is This A Good Question/Topic? 0
  • +

Replies To: JTable; automatically adjust column width

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

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.
Was This Post Helpful? 0
  • +
  • -

#3 gyron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 09-January 07

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?:D
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: JTable; automatically adjust column width

Posted 21 June 2007 - 08:15 AM

Hm, I forgot about the doublecliclk magic :o
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...
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

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.
Was This Post Helpful? 0
  • +
  • -

#6 gyron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 09-January 07

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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1