필터 지우기
필터 지우기

How do I create Listeners for my Java GUI in MATLAB?

조회 수: 13 (최근 30일)
How do I create Listeners for my Java GUI in MATLAB?
I created a Java GUI using MATLAB code in MATLAB, instead of creating and compiling a .java file. How do I create Listener objects, to associate with the objects in the GUI?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2010년 1월 22일
When using Java objects in MATLAB, you set callbacks for the objects that correspond to the Listeners you would associate with the objects in Java code.
import javax.swing.*;
% create the frame
JF = JFrame;
JF.setSize(300,300);
% create the button
JB = JButton('Press me');
JF.getContentPane.add(JB)
% this callback corresponds to the mouseClicked method of
% the MouseListener
set(JB,'MouseClickedCallback','disp(''pressed the button'')')
% display the frame
JF.setVisible(true)
The callbacks can be:
1) A string that will be executed as a MATLAB command in the base workspace
2) A function handle where the inputs to the function will always be the handle for the object, and event data, respectively.
3) A cell array where the first element is a function name or function handle. The first two inputs to the function are automatically the handle for the Java object, and an Event object. The other elements of the cell array are passed as the third, fourth, etc. inputs to the function.
Please note that listeners can only be created for classes accessible through the main Java search path defined by classpath.txt.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by