Thread Subject: How to color a specified line in LISTBOX

Subject: How to color a specified line in LISTBOX

From: Alric Gta

Date: 20 Apr, 2009 14:31:01

Message: 1 of 5

I have on my form:
-- 1 Listbox, 2 pushbuttons, 1 edit
=======================================
OBS:

Is this even posible and if yes how exactly somebody modifie my prg or give me some
example prg that do what i want.

Push Button 2 = Load a TXT file
Push Button 1 = Compare EDit/Listbox
=======================================
I want to :

a) Compare what is written in Edit with what a have in LIstbox
         --- this is done NO PRoblem Here.
b) When it match what is in the Edit with the line in LISTbox , -- I want that specific
    line in LIstbox to turn in GREEN === Big Problem
-------------------------------------------------------------------------------------------------------------
Here's a picture with what i want exact:

http://i361.photobucket.com/albums/oo54/Rikom/test.jpg

[IMG]http://i361.photobucket.com/albums/oo54/Rikom/test.jpg[/IMG]
==================================================
My Program:



% --- Executes just before Green_Detection is made visible.
function Green_Detection_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Green_Detection (see VARARGIN)

% Choose default command line output for Green_Detection
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Green_Detection wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Green_Detection_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

a=get(handles.edit1,'String');
prev_str = get(handles.listbox1,'String');
    len = length(prev_str);
   
for i=1:len
    set(handles.listbox1,'Value',i); %selecteaza in LIstBX val curenta cu care se compara
    if strcmp(prev_str(i),a)== 1


% set(handles.listbox1,'Value',('BackgroundColor','g'),i);

      
        return
    else
       if i==len % Daca nu sa gasit nici o potrivire
        errordlg('Nu sa gasit in Baza de Date!','Eroare');
       end
    end

 pause(1);
 end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)

[fname,pname] = uigetfile('*.txt');
X = textread(fullfile(pname,fname),'%s','delimiter','\n');
set(handles.listbox1,'string',X)

Thanx

Subject: How to color a specified line in LISTBOX

From: Yair Altman

Date: 13 May, 2009 01:19:02

Message: 2 of 5

simply set your listbox item with HTML colors. for example:
'<html><font color="red">my listbox item</font></html>'

Read this: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/

Yair Altman
http://UndocumentedMatlab.com
 

Subject: How to color a specified line in LISTBOX

From: Alric Gta

Date: 14 May, 2009 08:06:01

Message: 3 of 5

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <gud766$8dn$1@fred.mathworks.com>...
> simply set your listbox item with HTML colors. for example:
> '<html><font color="red">my listbox item</font></html>'
>
> Read this: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/
>
> Yair Altman
> http://UndocumentedMatlab.com
>

====================================
Thank you for your reply..... but is not exactly what I wanted.

I wanted to color the background of a SPECIFIC line in listbox
look at the links above with image.

NOT the WORD and NOt the entire background of the listbox

Subject: How to color a specified line in LISTBOX

From: Yair Altman

Date: 14 May, 2009 16:15:03

Message: 4 of 5

"Alric Gta" <alric2rei@yahoo.com> wrote in message <gugjd9$mbq$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <gud766$8dn$1@fred.mathworks.com>...
> > simply set your listbox item with HTML colors. for example:
> > '<html><font color="red">my listbox item</font></html>'
> >
> > Read this: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
> >
>
> ====================================
> Thank you for your reply..... but is not exactly what I wanted.
>
> I wanted to color the background of a SPECIFIC line in listbox
> look at the links above with image.
>
> NOT the WORD and NOt the entire background of the listbox

You'd need to write a custom Java ListCellRenderer class to achieve this (if you're already at it, this new class can also be used to display icons next to your list labels and do other fancy stuff).

Alternately, you could modify the listbox selection color and simply select the relevant entry/entries.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: How to color a specified line in LISTBOX

From: John

Date: 14 May, 2009 17:46:01

Message: 5 of 5

Try this -
'<html><DIV bgcolor="yellow"><font color="red">my listbox item</font></DIV></html>'

It will produce yellow background color for the specific listbox item.
You can do this when setting 'String' property value for each listbox item.



"Alric Gta" <alric2rei@yahoo.com> wrote in message <gsi0v5$g82$1@fred.mathworks.com>...
> I have on my form:
> -- 1 Listbox, 2 pushbuttons, 1 edit
> =======================================
> OBS:
>
> Is this even posible and if yes how exactly somebody modifie my prg or give me some
> example prg that do what i want.
>
> Push Button 2 = Load a TXT file
> Push Button 1 = Compare EDit/Listbox
> =======================================
> I want to :
>
> a) Compare what is written in Edit with what a have in LIstbox
> --- this is done NO PRoblem Here.
> b) When it match what is in the Edit with the line in LISTbox , -- I want that specific
> line in LIstbox to turn in GREEN === Big Problem
> -------------------------------------------------------------------------------------------------------------
> Here's a picture with what i want exact:
>
> http://i361.photobucket.com/albums/oo54/Rikom/test.jpg
>
> [IMG]http://i361.photobucket.com/albums/oo54/Rikom/test.jpg[/IMG]
> ==================================================
> My Program:
>
>
>
> % --- Executes just before Green_Detection is made visible.
> function Green_Detection_OpeningFcn(hObject, eventdata, handles, varargin)
> % This function has no output args, see OutputFcn.
> % hObject handle to figure
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
> % varargin command line arguments to Green_Detection (see VARARGIN)
>
> % Choose default command line output for Green_Detection
> handles.output = hObject;
>
> % Update handles structure
> guidata(hObject, handles);
>
> % UIWAIT makes Green_Detection wait for user response (see UIRESUME)
> % uiwait(handles.figure1);
>
>
> % --- Outputs from this function are returned to the command line.
> function varargout = Green_Detection_OutputFcn(hObject, eventdata, handles)
> % varargout cell array for returning output args (see VARARGOUT);
> % hObject handle to figure
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
>
> % Get default command line output from handles structure
> varargout{1} = handles.output;
>
>
> % --- Executes on selection change in listbox1.
> function listbox1_Callback(hObject, eventdata, handles)
> % hObject handle to listbox1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
>
> % Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
> % contents{get(hObject,'Value')} returns selected item from listbox1
>
>
> % --- Executes during object creation, after setting all properties.
> function listbox1_CreateFcn(hObject, eventdata, handles)
> % hObject handle to listbox1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles empty - handles not created until after all CreateFcns called
>
> % Hint: listbox controls usually have a white background on Windows.
> % See ISPC and COMPUTER.
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
>
> function edit1_Callback(hObject, eventdata, handles)
> % hObject handle to edit1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
>
> % Hints: get(hObject,'String') returns contents of edit1 as text
> % str2double(get(hObject,'String')) returns contents of edit1 as a double
>
>
> % --- Executes during object creation, after setting all properties.
> function edit1_CreateFcn(hObject, eventdata, handles)
> % hObject handle to edit1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles empty - handles not created until after all CreateFcns called
>
> % Hint: edit controls usually have a white background on Windows.
> % See ISPC and COMPUTER.
> if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
> set(hObject,'BackgroundColor','white');
> end
>
>
> % --- Executes on button press in pushbutton1.
> function pushbutton1_Callback(hObject, eventdata, handles)
>
> a=get(handles.edit1,'String');
> prev_str = get(handles.listbox1,'String');
> len = length(prev_str);
>
> for i=1:len
> set(handles.listbox1,'Value',i); %selecteaza in LIstBX val curenta cu care se compara
> if strcmp(prev_str(i),a)== 1
>
>
> % set(handles.listbox1,'Value',('BackgroundColor','g'),i);
>
>
> return
> else
> if i==len % Daca nu sa gasit nici o potrivire
> errordlg('Nu sa gasit in Baza de Date!','Eroare');
> end
> end
>
> pause(1);
> end
>
> % --- Executes on button press in pushbutton2.
> function pushbutton2_Callback(hObject, eventdata, handles)
>
> [fname,pname] = uigetfile('*.txt');
> X = textread(fullfile(pname,fname),'%s','delimiter','\n');
> set(handles.listbox1,'string',X)
>
> Thanx

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
color a specifi... Sprinceana 21 Aug, 2009 06:58:41
undocumented Yair Altman 12 May, 2009 21:24:03
rssFeed for this Thread

Contact us at files@mathworks.com