Listbox with multiple columns of text, and alignment of the text

4 views (last 30 days)
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.

Answers (1)

Walter Roberson
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
Matthew Brown
Matthew Brown on 3 May 2018
Thanks. The "|" is not really needed, I was just using it as visual separation between the name and group, which the alignment will suffice for, or to be used as a separator character later in the program.
For inserting HTML on each line: Because the 'String' property of the uicontrol has the value of a cell array, combinedNames, does this mean each entry into the cell array should be:
<table><tr><td align = "left">names{i}</td><td align = "right">groups{i}></table>
And then the listbox will know how to properly display that?
Thanks.

Sign in to comment.

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!