Toggle button background color unable to change

17 views (last 30 days)
Maury Lander-Portnoy
Maury Lander-Portnoy on 10 May 2015
Hi, I'm trying to a make a GUI where the default background color of a toggle button is black ([0 0 0]) and when it becomes pressed the background color changes to gray ([.5 .5 .5]). The text string in the button will be white but I've had no problems changing this so I'll put it aside for this question. I have tried both in 'guide' and in the actual code to specify the background color and its change but so far I have had no luck. Here's the code:
% --- Executes on button press in togglebutton11.
function togglebutton11_Callback(hObject, eventdata, handles)
if get(hObject,'value') == 1
set(hObject,'Backgroundcolor',[.5 .5 .5])
elseif get(hObject,'value') == 0
set(hObject,'Backgroundcolor',[0 0 0])
end
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton11
% --- Executes during object creation, after setting all properties.
function togglebutton11_CreateFcn(hObject, eventdata, handles)
set(hObject,'Backgroundcolor',[0 0 0])
% hObject handle to togglebutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
I have tried this same code with a push button equivalent (obviously without the 'value' variance) and it seems to work fine so I suppose MATLAB disables users from setting their own background color values for toggle buttons? HELP!
  1 Comment
Adam
Adam on 11 May 2015
Are both parts not working or just the callback which changes the colour?

Sign in to comment.

Answers (2)

Joseph Cheng
Joseph Cheng on 10 May 2015
Well i setup a simple example gui using your code and your code works for me. Things i would try is shut down matlab and try again. if problem still occurs then i would put a break point using the debugger at that if statement. then in the command window see what the values are and whether the sets are being conducted. One probably obvious issue is that togglebutton11 might not be the toggle button you are pushing. A probably dumb suggestion but it wouldn't be the first time it has come up and i couldn't say i haven't done the same when dealing with 11 controls just labeled 1 through 11.
  3 Comments
Walter Roberson
Walter Roberson on 11 May 2015
A) Try adding
else
fprintf(2, 'callback object value was not 1 or 0, was %g\n', get(hObject,'Value'));
B) try adding
drawnow()

Sign in to comment.


Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 11 May 2015
Edited: Alfonso Nieto-Castanon on 11 May 2015
I would first check if you are able to change the color of the togglebutton manually, e.g.
h = uicontrol('style','togglebutton','string','test');
set(h,'backgroundcolor','k');
If that does not work, then this is a java-related issue (some LAFs limit Matlab's ability to modify togglebutton's and popupmenu's bakground colors), and the only workaround that I know of is to change the Look-And-Feel of your uicontrol objects, for example using:
javax.swing.UIManager.setLookAndFeel('javax.swing.plaf.metal.MetalLookAndFeel')
(you may change the LAF of each object separately or globally for your entire gui, see Yair's undocumented matlab for additional info)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by