Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Calculate the size needed for a uicontrol

Asked by Arthur on 23 Jun 2011

I'm trying to calculate the actual size needed for uicontrols in a GUI so the GUI can resize itself appropriately. My problem is that the Extent property of a uicontrol is only the text area, and I can't find a way to determine the size of the surrounding control (such as the down arrow in a popup or the margin of an edit control). Is there a way to get the size of the decorations on a control?

I saw this related question, which looked like it ended with no solution as well.

0 Comments

Arthur

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 23 Jun 2011
Accepted answer

There is no documented way to do this. I do not know if there is a way to figure things out at the Java level.

What I ended up having to do was use trial and error to measure the widths of the various forms of decoration. I then wrote my own hgrightsize() routines that figures out the class of graphics element (e.g., text() vs uipanel() vs uicontrol) and dispatches to the appropriate "right sizing" subroutine.

I cannot give you the code, but I can tell you the experimental values for uicontrols, in pixels:

checkbox: 15
edit box: decoration 21 if multiline, 0 for single line
listbox: 19
popup: 16
pushbutton: 0
radiobutton: 15
slider: meaningless to "rightsize" a slider
textbox: 0
togglebutton: 0

There are some complications when you are determining the size of the text area itself via Extent: in some cases you should only look at the first line of a multiline String, and in other cases you need to find the widest line of the multiline string; in some cases the height of your box is one line and in other cases it should be the height of all of the lines. Those factors are easier to determine than the number of pixels of margin to allow for decorations.

1 Comment

Jan Simon on 27 Jun 2011

The dimensions of the sliders of a UILISTBOX depend on the display settings under Windows: Using a different scheme can modify the slider width also. Using the undocumented "get(H, 'PixelBounds')" does not helpe here, because it is equivalent to the usual "get(H, 'Position')" considering an extra margin of 6 pixels.

Walter Roberson

Contact us