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

Thread Subject:
Toggle button color in mac os X

Subject: Toggle button color in mac os X

From: Gaëlle

Date: 4 Jul, 2013 18:46:09

Message: 1 of 3

Hi,
I am desperately trying to make my toggle button switching background color depending on its state. The button stays in the 'default' color mode (i.e. dark and light grey when pushed and released respectively), even though I set it differently. The FontWeight and ForegroundColor work surprisingly well. This problem with setting the BackgroundColor does not appear with push buttons. I am working on mac os X Lion. My code is the following:

function testbuttons()
fh = figure;
uicontrol(fh,'units', 'normalized','Position',[0.4 0.55 0.5 0.037],...
    'String','Clu 1',...
    'Style','togglebutton',...
    'FontWeight','bold',...
    'ForegroundColor',[1 1 0],...
    'BackgroundColor',[1 0 0],...
    'Callback',@togglebutton1_Callback);


    function togglebutton1_Callback(hObject,eventdata)
        if get(hObject,'Value')
            set(hObject, 'BackgroundColor',[1 1 0])
        else
            set(hObject, 'BackgroundColor',[1 0 0])
        end
    end

end

Thank you for your help !

Subject: Toggle button color in mac os X

From: Yair Altman

Date: 4 Jul, 2013 19:13:10

Message: 2 of 3

Buttons are normally unaffected by the background color. This is controlled in most cases by the OS, except in rare cases like Win95 or in Windows Classic theme.

To get around this, you can
  * Set the button's CData property
  * Access the button's underlying Java object and update its properties - http://undocumentedmatlab.com/blog/button-customization/


Yair Altman
http://UndocumentedMatlab.com
 

Subject: Toggle button color in mac os X

From: Gaëlle

Date: 4 Jul, 2013 19:27:11

Message: 3 of 3

Hi,
I tried to change the CData property:

uicontrol(fh,'units', 'normalized','Position',[0.4 0.55 0.5 0.037],...
    'String','Clu 1',...
    'Style','togglebutton',...
    'CData',[0.9 0.1 0.1],...
    'Callback',@togglebutton1_Callback);

but I receive the following error:

Error using uicontrol
uicontrol CData property must be Truecolor

Moreover, I do not think that it is what I want, since for push buttons and toggle buttons, CData overlaps the String, and I want my string to be readable.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us