1

For my current project i am writing a JTable based GUI. One of the main features is the ability to adjust the sizes of all cells at runtime, depending on the contents (which change over time). Currently all cells have the same height and width, when the application is started. I would like to change that to a more sophisticated approach. I was wondering if it would be somehow possible to determine the space needed by "the content" to be displayed properly. That is without to much empty space or cutting something of.

"The content" is a string for starters. It is loaded from a database and i can't make any assumptions whatsoever about it. It may be null. In this case there should be any kind of default size for the corresponding cell.

In the long run there will be all different kinds of content to be displayed, like pictures, video and so on.

I tried working with FontMetrics to calculate the length of the strings. But since i'm using JTextPanes to display them, i can't get it to work exactly. I think this has to do with JTextPanes automatic word wrapping because sometimes the lines aren't filled up. This screws up my calculations.

Well long story short: I need some kind of design guideline to achieve the feature descriped above. I'm sure one of you clever guys knows just how to do it.

Thanks in advance, DeKay

user402026
  • 11
  • 1
  • What is your exact problem, the calculation or the display ? What do you want to customize, the height of given rows or the widht of given column ? – Manuel Selva Jul 26 '10 at 08:39
  • The Problem is the calculation of the required space. I want to customize both dimensions of the cell. The user should be able to click a cell and that cell should become larger. All this is implemented already. So again the problem is, how to determine how "big" the cells should get. – user402026 Jul 26 '10 at 08:54
  • And the dimensions of the cells depends only on the String content of the cell, is it right ? – Manuel Selva Jul 26 '10 at 09:08
  • For the time being, yes. It is going to work as follows: At the beginning all cells contain a certain string, read from a DB. A user would then click a cell, which is interpreted as interest in the corresponding dataset. Further details are collected from the DB. Therefore more text needs to be displayed which means the cell needs more display space and should be resized. A future version of my program should be able to present different kinds of content, not just text. So that has to be kept in mind. – user402026 Jul 26 '10 at 10:03

3 Answers3

1

As you are using JTextPane for rendering, you may find this Q&A helpful.

The conversion textPane.modelToView() always comes out to null.

Note that modelToView "Returns: the coordinates as a rectangle ... or null if the component does not yet have a positive size."

I have still no idea, how to calculate the amount of space needed in general.

IIUC, the key to understanding @camickr's example is the use of setPreferredSize() to include the text pane's changed boundary, followed by validate() which "is used to cause a container to lay out its subcomponents again."

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Although the example works fine, i can't get it to work in my context. The conversion textPane.modelToView(someInt) always comes out to null. Apart from that the approach doesn't solve my entire problem. I have still no idea, how to calculate the amount of space needed in general, that is without any asumptions about the component whatsoever. Futher help would be greatly appreciated. – user402026 Jul 27 '10 at 09:22
  • @dekay5555: I've elaborated above. – trashgod Jul 27 '10 at 15:48
1

Maybe the text pane size calculation in this How can I measure/calculate the size a Document needs to render itself? will help you out.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • @dekay5555: I've outline my understanding of @camickr's example in my answer, but I defer to his deeper understanding of the matter. – trashgod Jul 27 '10 at 15:53
-1

To set the height of a row in a JTable, look at here:

public void setRowHeight(int row, int rowHeight)

To set a column width, you have to look at the TableCOlumn API here:

public void setWidth(int width)

Hope this can help

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
  • Thank you. I'm aware of the API. Please have a look at the comment above, which hopefully clarifys my problem. – user402026 Jul 26 '10 at 08:55