How do I use javaObjectEDT and javaMethodEDT in MATLAB 7.8 (R2009a)?

8 views (last 30 days)
What is the use case for the Java API functions 'javaObjectEDT' and 'javaMethodEDT' and how do I use them?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Jun 2009
The functions 'javaObjectEDT' and 'javaMethodEDT' were introduced in MATLAB 7.8 (R2009a) and are not available in previous releases.
'javaObjectEDT' allows you to create an object on the Event Dispatch Thread (EDT) of Java. You can find this function useful if you are trying to create a Swing or AWT component in MATLAB that extends from the JComponent or Component classes respectively.
While it is very easy to create and access Java objects from MATLAB (including Swing objects), everything you do to them happens on MATLAB’s main thread, not the EDT. For example, the following code runs on the MATLAB main thread:
jf = javax.swing.JFrame();
jf.setName('foobar');
Having Swing methods execute on the MATLAB main thread can cause serious issues such as deadlocks while accessing components. Worse, they break the Swing rule of having Swing methods execute on the EDT.
Methods like javaObjectEDT and javaMethodEDT allow users to create objects and invoke methods on the EDT thread. Hence, the following code is executed on the EDT thread instead of the MATLAB thread:
f = javaObjectEDT('javax.swing.JFrame', 'New Title')
Similarly, the following method is executed on the EDT thread:
javaMethodEDT('setDefaultLookAndFeelDecorated', 'javax.swing.JFrame', true)
Further, if an object was created on the EDT thread using javaObjectEDT, all the following method invocations on that object happen on the EDT thread as well. You do not need to use javaMethodEDT for invoking methods on such objects.
For more information on Event Dispatch Thread, please view the following web pages:

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!