views:

48

answers:

3

Hello.

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

A: 

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
Thank you. I'm aware of the API. Please have a look at the comment above, which hopefully clarifys my problem.
A: 

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."

trashgod
Thanks, i'll have a look.
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.
@dekay5555: I've elaborated above.
trashgod
A: 

Maybe the text pane size calculation in this http://stackoverflow.com/questions/3315681/how-can-i-measure-calculate-the-size-a-document-needs-to-render-itself will help you out.

camickr
It's the same as trashgod's Link, but thanks anyway.
@dekay5555: I've outline my understanding of @camickr's example in my answer, but I defer to his deeper understanding of the matter.
trashgod