Adding callbacks to JFileChooser in embedded GUI

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

Accepted Answer

Malcolm Lidierth
Malcolm Lidierth on 23 Jul 2012
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 Comments
Malcolm Lidierth
Malcolm Lidierth on 24 Jul 2012
Edited: Malcolm Lidierth on 24 Jul 2012
@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 on 1 Aug 2012
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

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!