0

I wrote a utility to display the disk space occupied by folders in a folder tree. It uses a JTable with columns for folder name, space occupied, percentage of space occupied, number of files, etc.

I want to implement the standard feature of resizing the column to the minimum width required for the widest string displayed when the user double-clicks on the border between a column and its next column. I looked up examples of how to do this, and they work fine for columns that contain and display a string.

My "folder name" column prepends an icon to the folder name; the graphic in the icon indicates whether the folder is collapsed, expanded, or has no children, and is set to a width to represent how deep it is in the folder hierarchy (child folders are indented from their parents, etc.)

My "Size" column uses a value in bytes and converts it to a value of 1-3 digits followed by a space or letter indicating whether it is in bytes, kilobytes, megabytes, or gigabytes.

So when the code determining the width of the column is executed, it gets the width of the string, but in these two cases the width of the string is not the right width to which to set the column; for one of them, and in fact in general for JTable cells, just getting the string width cannot give the width of the column.

So how can I get the width of the minimum required display of the contents of a JTable cell, even when the contents are not a string?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
arcy
  • 12,845
  • 12
  • 58
  • 103
  • 1
    See: [How do I make a JTable column size exactly (or closely) fit contents?](https://stackoverflow.com/questions/31977754/how-do-i-make-a-jtable-column-size-exactly-or-closely-fit-contents/31977776#31977776) – camickr Mar 13 '21 at 16:29
  • Thanks to @camickr - the actual answer to my question (embedded in his/her reference) is that JTable has the method prepareRenderer(); this returns the cell's component, which can then be queried for size and such. – arcy Mar 13 '21 at 21:56

0 Answers0