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 CenterFile Exchange에서 Introduction 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