How to find the minimum width to display the window title completely


April 2019


32 time


I am maintaining some existing Java code so I am not looking to make major changes to the way it is currently done. I removed some items from a dialog, (which is fundamentally an org.eclipse.swt.widgets.Shell) and now the dialog displays much smaller, so small that the title and the menu bar do not display in their entirety. I can set the minimum width to fix this problem, but since the users may have different screen sizes and resolutions, I would prefer not to attempt a one-size-fits-all width.

I set it to the preferred size, using




and it improved the situation (the menu displays completely), but the dialog title is still being elided.

Is there a way of determining the minimum width of a dialog to display the title completely?

Alternatively, is there a way of getting the title as it's displayed (including ellipsis, if any) or of determining if it's being elided?

1 answers


IFAIK, there is no way to compute the exact size of a shell so that its title can always be shown unshortened.

While the width of a string can be computed with a graphics context like this:

GC gc = new GC(shell);
Point titleSize = gc.textExtent("title");
int titleWidth = titleSize.x;

there is no way to determine a shell's title trim. That is, the space occupied by the close, minimize, and maximize buttons and the system menu (if visible at all).

To get a hint though, it should be possible to use shell.computeTrim(0, 0, titleSize.x, titleSize.y). The returned rectangle's width field should give an approximation of the width needed for the shell.