I want to place some text in a GUI, and I want to know the exact size the uicontrol
of type \'text\'
needs to be!
I\'ve found several threa
Observing the Java side of things, Swing components have several methods of interest:
getVisibleRect
)The thing is, that the "preferred size" seems to be the correct size (which you seek), whereas the size returned by get(...,'Extent');
is the visible size, which has the following meaning:
getVisibleRect()
Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()), and all of its ancestors' visible rectangles.
To clarify the above: theme- and platform-specific decorations of the figure window may decrease the available space of the component, and therefore its visible size (as mentioned here).
As a numeric example, when running with default settings and repmat('A',14)
, I get (on Win7, MATLAB 2015a):
get(u,'Extent')
- [0,0,116,214]
jHandle.getVisibleRect
- java.awt.Rectangle[x=0,y=0,width=116,height=214]
jHandle.getSize
- java.awt.Dimension[width=116,height=214]
jHandle.getPreferredSize
- java.awt.Dimension[width=116,height=221]
Now the question is how to get PreferredSize
(or jHandle
from which it may be retreived) conveniently...
One option, which I used, is the findjobj utility, whose usage is as simple as jHandle = findjobj(u)
.
To summarize:
Replace the two lines where you find textsize
by this:
v = findjobj(u); textsize = [v.getPreferredSize.getWidth v.getPreferredSize.getHeight];
PROFIT.
P.S.
My reasoning may be flawed and understanding of Swing incorrect, however this explanation makes sense to me and more importantly - it works.