MATLAB Answers

Robert Cumming

jchooser embeded in GUI - obtain status of buttons and change to save type

Asked by Robert Cumming
on 7 Aug 2012

This is a follow on from a previous question.

I am trying to embed a uifile load/save file dialog into another GUI, I'm having issues determining which button was pressed (load/save or cancel) and changing the "open" button to say "Save"

How can I:

1. In the ActionPerformedCallback obtain the jchooser status (i.e. which button was pressed)

2. Change the jchooser to be a save dialog rather than open.


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.9 0.8 0.1 0.2]);
set(hPanel, 'units','normalized','position',[0 0 0.9 1.0]);
jchooser = javaObjectEDT('javax.swing.JFileChooser', pwd );
set(handle(jchooser, 'callbackproperties'),'ActionPerformedCallback', 'disp(''Action'')')
% Show the actual dialog


1 Answer

Answer by Malcolm Lidierth
on 16 Aug 2012
Edited by Malcolm Lidierth
on 16 Aug 2012
 Accepted answer


Replace 'disp(''Action'')' with a callback

function callback(hObj,evt)

Within the callback, evt.getActionCommand() will give the button e.g.


will return true for the OK button and


for cancel.

For dialog types use e.g.


Google "grep JFileChooser" to get links to the source code for JFileChooser.

  1 Comment

Robert Cumming
on 28 Aug 2012

excellent Batman....

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!