Listbox with multiple columns of text, and alignment of the text
4 views (last 30 days)
Show older comments
Hello,
I am working on a program that has a GUI, and contains a listbox. The fields in the listbox are populated with data that is read from a CSV file. One column of the CSV is the "name" field, and one column is the "group". The original code only had to display the "name" field, and that works without a hitch. I am now trying to display both of those at the same time, inside the listbox, and have the 1st column be aligned to the left of the listbox, the second column aligned on the right.
Here is an example of my current code:
nameTable= loadFromFile(fileName);
names = nameTable{1};
groups = nameTable{4}; %Get all the groups, in the order that correlate with name
uniqueGroups = unique(groups); %Get just the unique groups, to be used for a dropdown filter later
combinedNames= {};
for i=1:lengthnames
combinedNames{i} = strcat(names{i},'|',groups{i});
end
.
.
.
.
nameListbox= uicontrol('Style','listbox','Tag','nameListbox','Value',[],...
'Units','pixels','BackgroundColor',COLORS.textbg,...
'Position',[740 425 185 130],'FontName',FONTNAME,...
'String',combinedNames,'Max',2.0,'Callback',@nameListbox_Callback,...
'Tooltip','Control-click for multiple selections');
Right now, this makes the listbox look like this:

And I would like it to look like this (please excuse bad photo-editing)

I'm not sure how to accomplish this. I know that the 'combinedNames' string is concatenated together without the spacing, causing it to display how it currently is. Any assistance or ideas to get it displaying as desired will be appreciated.
Thanks.
1 Comment
Walter Roberson
on 3 May 2018
Note that the | is only obeyed when you use a single string, not a cell array.
Answers (1)
Walter Roberson
on 3 May 2018
You will need to use HTML for the entries, using table/ entries with two cells each of which has an alignment specified. You will need to do this independently for each line as the HTML only applies line by line.
Or... You can use a fixed width font, in which case the space is significant.
2 Comments
See Also
Categories
Find more on Downloads in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!