How use Pop-Up Menu with including Check Box?

52 views (last 30 days)
How use Pop-Up Menu with including Check Box?
I would like to use Pop-Up Menu with Check Box.
In general, When I check PopUp Menu, then show the List.
But, I'd like to make List in CheckBox Style
Please give me some solution or answer.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Oct 2011
A pop-up menu cannot include a check box (not unless perhaps you work at the Java level.)
You can have a push-button that turns on the visibility of a radiobutton group.
  1 Comment
Jun-Hyeong kim
Jun-Hyeong kim on 21 Oct 2011
working java level means hard coding?
And if you have any example solution, could you provide it to me.

Sign in to comment.

More Answers (3)

Jan
Jan on 21 Oct 2011
You can use a uicontextmenu, which has built-in checkmarks. It can be enabled by a left mouse click on any other uicontrol, e.g. a button. The position of the uicontextmenu should be set relative to the button to let it look more as a popup menu:
FigH = figure;
PopH = uicontextmenu('Parent', FigH);
uimenu(PopH, 'Label', 'Unchecked');
uimenu(PopH, 'Label', 'Checked', 'Checked', 'on');
ButtonH = uicontrol('Style', 'PushButton', 'Position', [20, 300, 60, 22], ...
'String', 'Click me', ...
'UIContextMenu', PopH, ...
'Callback', {@showMyPopup, PopH});
function showMyPopup(ButtonH, EventData, PopH)
Pos = get(ButtonH, 'Position');
set(PopH, 'Position', [Pos(1)-2, Pos(2)-1], 'Visible', 'on');
Currently this is a pop- down menu. At least it need no calls to undocumented functions.

Daniel Pantea
Daniel Pantea on 6 Feb 2020
Edited: Daniel Pantea on 6 Feb 2020
CheckBoxListComboBox.png
Simple example of "CheckBoxListComboBox" usage:
f = figure;
% create CheckBoxListComboBox
jCB = com.jidesoft.combobox.CheckBoxListComboBox({'Item-1', 'Item-2', 'Item-3', 'Item-4', 'Item-5', 'Item-6', 'Item-7', 'Item-8'});
[hJavaCB, hJavaCBWrapper] = javacomponent(jCB, [], f); %#ok
set(hJavaCBWrapper, ...
'Units', 'pixels', ...
'Position', [4,4,460,20]);
set(hJavaCB, ... % modify CB properties
'ToolTipText', ...
['<html>Supports <b>multiple</b> selections via <i><font color="red">drop-down</font></i> list + OK, or by editing directly (but ensure ' ...
'<br /> correct spelling and that there is exactly one semicolon followed by at least one space "; " between items)</html>'], ...
'Name', 'Display range' ...
);
% direct access
hJavaCB.setSelectedIndices([0,1,4]);
xx = hJavaCB.getSelectedIndices(); % xx = [0,1,4]
% indirect access via model
jModel = hJavaCB.getModel();
sz = jModel.getSize(); % sz = 8
jModel.getElementAt(0); % ans = 'Item-1'
xx = jModel.getSelectedItem(); % ans = java.lang.Object[]: 'Item-2'; 'Item-5'; 'Item-8'. Use numel(xx), strcmp(xx(1),'Item-2'), etc
jModel.getIndexOf('Item-2'); % ans = 1
jModel.insertElementAt('Item-99', 3); % inserts between 'Item-3' (on pos 2) and 'Item-4' (on pos 3)
jModel.addElement('Item-100'); % append to end
%jModel.removeAllElements();
jModel.removeElement('Item-1');
jModel.removeElementAt(3);
jModel.setSelectedItem(['Item-5';'Item-6';'Item-7']);
hJavaCB.setFocusable(true);
hJavaCB.putClientProperty('TabCycleParticipant', true); % this may be unnecessary in some cases, but doesn't hurt
set(hJavaCB, ...
... % handles keys like VK_LEFT, etc
'KeyPressedCallback', {@callback_javaKeyPress}, ...
... % handles any selection changes, even temporary ones, before OK or Cancel!
'PropertyChangeCallback', {@callback_javaPropertyChange}, ...
... % handles only final selection changes, after Enter, OK or Cancel...
'ActionPerformedCallback', {@callback_javaActionPerformed} ...
);
% 'KeyPressedCallback' from hJavaCB
function callback_javaKeyPress(h, event)
switch event.getKeyCode()
case event.VK_LEFT
character = 'left';
otherwise
character = event.getKeyChar();
end
% character, event
end % function callback_javaKeyPress
% 'PropertyChangeCallback' from hJavaCB
function callback_javaPropertyChange(h, e)
%disp(' callback_javaPropertyChange ');
%h, e
%list = h.getSelectedIndices(); % not the indices are 0-based, and -1 means invalid field
%fprintf(1, 'callback_javaPropertyChange: ');fprintf(1, ' %d', list); fprintf(1, '\n');
end % function callback_javaPropertyChange
% 'ActionPerformedCallback' from hJavaCB
function callback_javaActionPerformed(h, e)
list = h.getSelectedIndices(); % not the indices are 0-based, and -1 means invalid field
fprintf(1, 'callback_javaActionPerformed: ');fprintf(1, ' %d', list); fprintf(1, '\n');
h, e
end % function callback_javaActionPerformed
Tested with 2017a and 2020a ...
  1 Comment
Ganesh Naik
Ganesh Naik on 16 Dec 2021
Hi Daniel thanks for this wonderful tool. While running the code using Matlab 2021a version it gave me the following warning:
Warning: JAVACOMPONENT will be removed in a future release. For more information see UI Alternatives for MATLAB Apps on mathworks.com.
> In javacomponent (line 85)
In Popup_Checkbox (line 4)
I tried to find the alterantive using the following website
Could you please let me know whether do I need to work with UIfigure instead?

Sign in to comment.


amir soheil
amir soheil on 12 Jan 2016
checkboxcombo class in java programing is best...refer to undocument matlab website

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!