When I click on a Java button object in MATLAB, why does it not evaluate its callback?

조회 수: 10 (최근 30일)
When I click on a Java button object in MATLAB, why does it not evaluate its callback?
The callback function for my Java button object is not responding to the button click.
What is the correct way to implement this?
I am using the following code:
function my_java_test()
import java.awt.*
import java.awt.event.*
import javax.swing.*;
f = JFrame(sprintf('Houston... we have a problem'));
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
b = JButton('Hello');
set(b,'ActionPerformedCallback', {@doHelloButtonClicked});
f.getContentPane.add(b)
f.pack
f.show
function doHelloButtonClicked(src, data)
src.setLabel(sprintf('Roger that... pizza has been ordered'));

채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
This issue can be resolved by explicitly passing the handle of the JButton object to the callback function. This will be the callback function's third input argument.
This is illustrated below:
function my_java_test()
import java.awt.*
import java.awt.event.*
import javax.swing.*;
f = JFrame(sprintf('Houston... we have a problem'));
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
b = JButton('Hello');
% set(b,'ActionPerformedCallback', {@doHelloButtonClicked});
set(b,'ActionPerformedCallback', {@doHelloButtonClicked, b}); % Here, b is passed as an argument
f.getContentPane.add(b)
f.pack
f.show
function doHelloButtonClicked(src, data, b ) % Now, b is the third argument to the callback function
b.setLabel(sprintf('Roger that... pizza has been ordered'));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by