Adding callbacks to JFileChooser in embedded GUI

2 ビュー (過去 30 日間)
Robert Cumming
Robert Cumming 2012 年 7 月 23 日
Hi, I'm trying to build a dialog with an embedded file open dialog (see code below as starting point). I'm having trouble working out how to add callbacks (listerners) to the open and cancel buttons.
I want to add a number of checkboxes to the right hand side - and if the user selects a file/files and clicks open then I want to perform specific tasks - any help on how I can do this much apprecaited.
import javax.swing.JFileChooser;
d = dialog('windowstyle', 'normal', 'position', [0 0 750 400] );
centerfig ( d )
set ( d, 'visible', 'on' );
[jPanel,hPanel] = javacomponent(javax.swing.JPanel, [], d);
hControl = uicontrol('style','checkbox', 'parent',d,'string','click me', 'units','normalized','position', [0.85 0.8 0.2 0.2]);
set(hPanel, 'units','normalized','position',[0 0 0.8 1.0]);
jchooser = javaObjectEDT('javax.swing.JFileChooser', pwd );
%%Show the actual dialog
% status = jchooser.showOpenDialog([]);
jPanel.add(jchooser)

採用された回答

Malcolm Lidierth
Malcolm Lidierth 2012 年 7 月 23 日
You could install callbacks to log the state of the buttons in MATLAB so these would be available after you click 'Open' and return to MATLAB.
hControl = uicontrol('style','checkbox', 'parent',d,'string','click me', 'callback', 'disp(''State Change'')','units','normalized','position', [0.85 0.8 0.2 0.2]);
Replace
'disp(''State Change'')'
with a callback.
  3 件のコメント
Malcolm Lidierth
Malcolm Lidierth 2012 年 7 月 24 日
編集済み: Malcolm Lidierth 2012 年 7 月 24 日
@Robert
Both buttons fire ActionPerformed events so
set(jchooser,'ActionPerformedCallback', 'disp(''Action'')')
Edit: For future reference, that would be better as:
set(handle(jchooser, 'callbackproperties'),'ActionPerformedCallback', 'disp(''Action'')')
to reduce memory leaks and MATLAB warnings on recent versions.
Robert Cumming
Robert Cumming 2012 年 8 月 1 日
Malcolm,
Thanks for this 1 have 2 further questions.
1. How to obtain which of the actions was performed?
2. How to change the jchooser to be a save dialog rather than opendialog
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by