How use Pop-Up Menu with including Check Box?

16 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2011-10-21
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 个评论
Jun-Hyeong kim
Jun-Hyeong kim 2011-10-21
working java level means hard coding?
And if you have any example solution, could you provide it to me.

请先登录,再进行评论。

更多回答(3 个)

Jan
Jan 2011-10-21
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 2020-2-6
编辑:Daniel Pantea 2020-2-6
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 个评论
Ganesh Naik
Ganesh Naik 2021-12-16
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?

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by