5

I have a GUI with big buttons and wouls like to align the text in the button to the top, all I found is "horizontal alignment" property. Thanks...

Amro
  • 123,847
  • 25
  • 243
  • 454
A S
  • 2,924
  • 5
  • 22
  • 27

2 Answers2

7

You need to access the underlying Java Swing component (I am using FINDJOBJ function):

figure('Menubar','none', 'Position',[200 200 300 200])
h = uicontrol('Style','pushbutton', 'String','click', ...
   'Units','normalized', 'Position',[0.3 0.3 0.5 0.5]);
jh = findjobj(h);
jh.setVerticalAlignment( javax.swing.AbstractButton.BOTTOM );

enter image description here

Amro
  • 123,847
  • 25
  • 243
  • 454
  • +1 @Amro, I thought when I scrolled down it would be an answer from Yair Altman. Much nicer than my proposed hacks. – Sam Roberts Nov 23 '11 at 09:59
  • it's true I learned a lot from Yair's [blog](http://undocumentedmatlab.com/) on all things *undocumented* – Amro Nov 23 '11 at 14:17
  • @Amro Thank you, it's working! Do you know a way to align only text and not image placed by: set(handles.button,'cdata',icon); Thanks! – A S Nov 24 '11 at 08:11
  • @Amro, really nice thanks, for those wondering the other way like me you can swap out jh.setVerticalAlignment( javax.swing.AbstractButton.BOTTOM ); with jh.setHorizontalAlignment( javax.swing.AbstractButton.LEFT ); – MoustacheDangerous May 03 '17 at 17:39
0

I'm afraid I think you can't do this - text is always vertically aligned at the middle on a uicontrol. The only hacks I can think of that might achieve something like what you want are

  1. Add extra return characters after your main text, so that the real text ends up at the top while the whole text remains centred
  2. (REALLY horrible) Create an image with your text right at the top, and use this with the CData property of the button.
Sam Roberts
  • 23,951
  • 1
  • 40
  • 64