Search code examples
draw2d

Gef/draw2d: How to get the height of word wrapped text


So I'm making a draw2d program. I have some inputted text which I word wrap by doing:

    TextFlow textWrapper = new TextFlow(_title);
    FlowPage flowPage = new FlowPage();
    flowPage.add(textWrapper);

Now I need to find the height of the text inside it's container with a fixed width. So far I haven't been able to find a way to do so. Other threads I've looked at have mentioned using the getPreferredSize() method but this fails to return the correct height.

Any advice would be helpful, thanks.


Solution

  • You should be able to get this value by invoking getPreferredSize(<your width>, -1). Worked for me, as you can read here. Just remember that you have to invalidate the container for it to work, otherwise it will take values from it's internal cache (at least that is what I remember).