3

I am creating GUI and I want to have inputs and results there.

I have text fields as labels and edit or popup for inputs. When I have

uicontrol('style','text','string','foo','units','centimeters','position',[1 1 1 0.5]);
uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 0.5]);

I get the foo strings slightly misaligned, baseline of the text field is slightly above the baseline of edit/pop field.

How can I align those fields?

Crowley
  • 2,279
  • 6
  • 22
  • 36
  • 1
    To vertically center the text in the `text uicontrol` you will need to access the underlying Java directly. [This post](http://stackoverflow.com/questions/8225543/how-to-align-text-to-the-top-of-the-button-in-matlab-gui) shows the general approach. Modifying the background color of the `edit uicontrol` has no affect, MATLAB forces the color to White while the control is enabled. – sco1 Mar 08 '16 at 20:00
  • @excaza Would you like to expand it to the answer? – Crowley Mar 08 '16 at 20:03

2 Answers2

5

Unfortunately this requires accessing the underlying Java. The method is similar to Amro's approach here for a pushbutton and utilizes the external findjobj function:

h.t = uicontrol('style','text','string','foo','units','centimeters','position',[1 1 1 0.5]);
h.e = uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 0.5]);
jh = findjobj(h.t);
jh.setVerticalAlignment(javax.swing.JLabel.CENTER)

Unfortunately, this is still off by a pixel or two:

yay

I'd say just bump the text box by pixel, as necessary:

oldunits = get(h.t, 'Units');
set(h.t, 'Units', 'Pixels');
pos = get(h.t, 'Position');

pos(2) = pos(2) + 1;

set(h.t, 'Position', pos)
set(h.t, 'Units', oldunits)

Which gives us:

yay2


Edit: Modifying the BackgroundColor property of the edit box has no effect (though setting it to none makes it a black box...), and the box will remain the default color. Per The MathWorks, this is a design decision:

This is a expected behavior in the way that MATLAB displays the BackgroundColor for editable text objects.

This could also most likely be updated by leveraging the underlying Java, but I'm not familiar.

Community
  • 1
  • 1
sco1
  • 12,154
  • 5
  • 26
  • 48
1

Another option is to use the Jlabel, which is a Javacomponent. I have found the uicomponent from undocumentedMatlab helpful. You can download it from Mathworks File Exchange using this link:https://uk.mathworks.com/matlabcentral/fileexchange/14583-uicomponent-expands-uicontrol-to-all-java-classes

See this example:

figure(1);clf;

[txt1, txt2] = uicomponent(gcf, 'style','JLabel'); % simple spinner with initial value
set(txt1, 'Text', 'Test', 'units','centimeters','position',[1 1 1 1]);
set(txt2, 'horizontalAlignment', javax.swing.SwingConstants.LEFT);
set(txt2, 'VerticalAlignment', javax.swing.SwingConstants.CENTER);

uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 1]);

It can create a UI like below:

enter image description here

shehperd
  • 59
  • 4