Search code examples
matlabuser-interfacebuttonborderborderless

How to make a simple borderless button in MATLAB GUI


Quite simply, I'm trying to create a borderless button in a MATLAB GUI. The reasons are mostly aesthetics, so no need for a debate on why it should be borderless.

I already know that this cannot be done using the built-in MATLAB uicontrol alone, since the border of the button is not an accessible property in MATLAB. Thus, the underlying JAVA code (upon which MATLAB is written), must be accessed in order to manipulate the border. This is where I get lost, since I've only ever programmed in MATLAB.

I followed an example from here: http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties

But I'm still not getting a borderless button.

Here is a simple code example (NOTE the use of Yair Altman's findjobj which is available on the MATLAB file exchange):

f=figure('Menubar','none', 'Position',[200 200 300 200]);
p=uipanel(f, 'BackgroundColor', [0 0 1]);
h = uicontrol('parent', p, ...
'Style','pushbutton', ...
'String','click', ...
'TooltipString', 'you should click this' ...
'Units','normalized', ...
'Position',[0.3 0.3 0.5 0.5], ...
'BackgroundColor', [0 0 1]);
jh = findjobj(h);
jh.setBorder(javax.swing.BorderFactory.createEmptyBorder()); 
%attempt 1 does not remove border
jh.border=[];                                                
%attempt 2 does not remove border
jh.setBorder([]);                                            
%attempt 3 does not remove border
jh.border=javax.swing.BorderFactory.createEmptyBorder();     
%attempt 4 does not remove border

Any thoughts on where I've gone wrong? Thanks!


Solution

  • You shoud add two lines:

    jh.setBorderPainted(false);    
    jh.setContentAreaFilled(false);