controlling background color of selected uicontrol popup?

I can create a uicontrol('Style','popup') with a 'backgroundcolor'. This works and the color is the one I chose. However, as soon as I select any value, the background color goes to a grayish, with the text in the foreground color I have set. The selected pop-up entry does not use the background color.
Is there a way to control the background color of a selected pop-up entry?
(I am trying to color-code my uicontrols according to the major class of functionality they have within the terms of my program. Although I could in theory do so just by adjusting their text foreground colors, the area of the text compared to the area of the control is relatively small and the color coding of the text is not nearly as distinguishable as color coding the background.)

3 commentaires

Dang. Jan's technique worked for popups, but not for a list box. I had to force it to unrender (by setting max to 1 on a multivalued list) and rerender. That emits a warning each time, so I don't want to handle things that way. Oh yes, changing the box size massively wasn't enough of a re-rendering!
Jan
Jan le 15 Fév 2011
Does REFRESH(FigHandle) help?
Alas, refresh() doesn't do a thing. It doesn't even cause the figure to flash momentarily if I'm stepping in debug mode.

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 28 Jan 2011
Modifié(e) : Jan le 30 Jan 2013
The selected pop-up gets the different color as long as the UICONTROL has the focus. Although Matlab's documentation claims that "figure(gcf)" gives the focus to the figure, this does not work from at least Matlab 5.3 to 2009a. Therefore I usually insert a call to a function named "FocusToFig" in the UICONTROL's callback function:
function FocusToFig(ObjH, EventData) %#ok<INUSD>
% Move focus to figure
% FocusToFig(ObjH, [DummyEventData])
% INPUT:
% ObjH: Handle of a graphics object. It is tried to move the focus to the
% parent figure and making it the CurrentFigure of the root object.
% DummyEventData: The 2nd input is optional and ignored.
%
% Tested: Matlab 6.5, 7.7, 7.8, WinXP
% Author: Jan Simon, Heidelberg, (C) 2009-2011
if any(ishandle(ObjH)) % Catch no handle and empty ObjH
FigH = ancestor(ObjH, 'figure');
if strcmpi(get(ObjH, 'Type'), 'uicontrol')
set(ObjH, 'enable', 'off');
drawnow;
set(ObjH, 'enable', 'on');
end
% Methods according to the documentation (does not move the focus for
% keyboard events under Matlab 5.3, 6.5, 2008b, 2009a):
figure(FigH);
set(0, 'CurrentFigure', FigH);
end
return;
This moves the keyboard focus also to the figure, such that the KeyPressFcn is called again.

2 commentaires

The grayish color is retained even when focus moves elsewhere. A uicontrol popup that has focus has a single-pixel rectangle of color drawn around the outside of it (it looks orange-y at least with the controls I tested on.)
Jan
Jan le 29 Jan 2011
The main task of my function for your purpose is not to move the focus, but to disable the UICONTROL temporarily, what forces a redraw.
Because the moving of the focus does not work correctly under Windows for 10 years, I expect there could be corresponding problems for a Ubuntu64 server displayed to a Linux32 client...

Connectez-vous pour commenter.

Plus de réponses (2)

Things seem to work here as you wish, unless I misunderstand you.
uicontrol('Style','popup','backgroundcol',[.9 .7 .3],'string',{'P';'Y';'F'})
This creates a popupmenu with an orange-ish bgc. After I select any of the three strings, the bgc is the same.

6 commentaires

2008b, Linux-64 (Ubuntu) server displayed to Linux-32 (Ubuntu), after I select any of the strings, the background turns to gray . The control is outlined in orange as long as the control has focus.
If set() the background of the control when it has something selected by the user, nothing changes. If I set() the Value of the control, the appropriate entry comes up but the color is still wrong.
But! If I set the Value of the control so that it does not render (generating a warning) and then set the Value back again, then when the control re-appears it will have the appropriate entry and the appropriate background color. And if I then set the Value to something else without clicking on it, the new appropriate entry is displayed, still with the right background.
Thus this has something to do with the clicking rather than the change in Value.
Perhaps it is an (ahem) "undocumented feature" for this configuration.
Jan
Jan le 29 Jan 2011
And this is exactly what my suggested method does: Disable the uicontrol temporarily, what forces a redraw. The moving of the focus is just a side-effect. Did you try my function?
Walter Roberson
Walter Roberson le 29 Jan 2011
Modifié(e) : Walter Roberson le 2 Août 2021
Jan, you are right, disable / drawnow / enable does get the color back to what it should be.
s/write/right/
(We need a commented editor!)
Aggh. We need a *comment* editor!
Jan
Jan le 29 Jan 2011
Fine. Is your problem solved then?

Connectez-vous pour commenter.

Daniele Scaranari
Daniele Scaranari le 31 Oct 2018

0 votes

figure(gcf) works correctly in Matlab R2017a, Windows10.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by