Adding callbacks to JFileChooser in embedded GUI

2 vues (au cours des 30 derniers jours)
Robert Cumming
Robert Cumming le 23 Juil 2012
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)

Réponse acceptée

Malcolm Lidierth
Malcolm Lidierth le 23 Juil 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 commentaires
Malcolm Lidierth
Malcolm Lidierth le 24 Juil 2012
Modifié(e) : Malcolm Lidierth le 24 Juil 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 le 1 Août 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by