Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Modal JDialog problems

Subject: Modal JDialog problems

From: Praetorian

Date: 19 Mar, 2009 19:12:25

Message: 1 of 12

Hi,
I have a MATLAB GUI with a pushbutton that calls a java class. The
java class simply extends JDialog and shows a popup dialog when the
MATLAB pushbutton is clicked. There seems to be a problem with making
this popup dialog modal. If it is modal then everything hangs as soon
as I try to move the dialog even a little bit. There is no problem if
the dialog is non-modal. Has anyone else faced this problem?

The reason I'd like to have it as a modal dialog is to prevent the
user from interacting with the parent MATLAB GUI while this dialog is
open. Is there another way to achieve this?

Thanks in advance,
Ashish.

Subject: Modal JDialog problems

From: Zhelyazko

Date: 19 Mar, 2009 22:30:19

Message: 2 of 12

I tried it as modal dialog and it worked without a problem.

function modal
f=figure;
h=uicontrol(...
'style','pushbutton',...
'String','Show Dialog',...
'Units','Norm',...
'Pos',[0,0,1,1],...
'Callback',@createDialog);
function createDialog(varargin)
f=javax.swing.JFrame;
jD=javax.swing.JDialog(f,'Click a button',1);
jOP=javax.swing.JOptionPane('Some text', javax.swing.JOptionPane.QUESTION_MESSAGE,javax.swing.JOptionPane.YES_NO_OPTION);
jD.setContentPane(jOP);
jD.pack();
jD.setVisible(1);
f.pack();
f.show();



But if you want it to be a non modal I guess you can loop while the dialog is showing.
function modal
f=figure;
h=uicontrol(...
'style','pushbutton',...
'String','Show Dialog',...
'Units','Norm',...
'Pos',[0,0,1,1],...
'Callback',@createDialog);
function createDialog(varargin)
f=javax.swing.JFrame;
jD=javax.swing.JDialog(f,'Click a button',0);
jOP=javax.swing.JOptionPane('Some text', javax.swing.JOptionPane.QUESTION_MESSAGE,javax.swing.JOptionPane.YES_NO_OPTION);
jD.setContentPane(jOP);
jD.pack();
jD.setVisible(1);
f.pack();
f.show();
while jD.isShowing()
   
end

Subject: Modal JDialog problems

From: Yair Altman

Date: 19 Mar, 2009 22:36:01

Message: 3 of 12

> The reason I'd like to have it as a modal dialog is to prevent the
> user from interacting with the parent MATLAB GUI while this dialog is
> open. Is there another way to achieve this?


I can think of two ways:

1) use a Matlab dialog figure (use the dialog function or one of its variants or create your own) and make it modal via the figure's WindowStyle property. I think this is your best choice. Anything you can do with a JDialog you can also do with a Matlab figure (including using Swing components etc.).

2) use my "enable/disable figure" submission on the file Exchange (http://www.mathworks.com/matlabcentral/fileexchange/15895 ) to temporarily disable the main Matlab GUI until the dialog returns. The risk is that if the dialog crashes or what-not, you're stuck in disabled-mode for your main GUI, so you need to be extra careful about exception handling.

Yair Altman
http://Undocumented-Matlab.com
(it's finally live!)

Subject: Modal JDialog problems

From: Praetorian

Date: 20 Mar, 2009 00:37:34

Message: 4 of 12

On Mar 19, 4:36=A0pm, "Yair Altman" <altmany...@gmailDEL.comDEL> wrote:
> > The reason I'd like to have it as a modal dialog is to prevent the
> > user from interacting with the parent MATLAB GUI while this dialog is
> > open. Is there another way to achieve this?
>
> I can think of two ways:
>
> 1) use a Matlab dialog figure (use the dialog function or one of its vari=
ants or create your own) and make it modal via the figure's WindowStyle pro=
perty. I think this is your best choice. Anything you can do with a JDialog=
 you can also do with a Matlab figure (including using Swing components etc=
.).
>
> 2) use my "enable/disable figure" submission on the file Exchange (http:/=
/www.mathworks.com/matlabcentral/fileexchange/15895) to temporarily disable=
 the main Matlab GUI until the dialog returns. The risk is that if the dial=
og crashes or what-not, you're stuck in disabled-mode for your main GUI, so=
 you need to be extra careful about exception handling.
>
> Yair Altmanhttp://Undocumented-Matlab.com
> (it's finally live!)

Thanks for those suggestions Yair; the first one is exactly what I
need. I'm still using my class instead of loading uicontrols into the
dialog directly because I'm displaying a JTree amongst other things.
Thanks again for your help and for posting UICOMPONENT!

Subject: Modal JDialog problems

From: Yair Altman

Date: 20 Mar, 2009 01:31:01

Message: 5 of 12

> Thanks for those suggestions Yair; the first one is exactly what I
> need. I'm still using my class instead of loading uicontrols into the
> dialog directly because I'm displaying a JTree amongst other things.
> Thanks again for your help and for posting UICOMPONENT!

If all you need is a JTree, then you could try using Matlab's uitree function. It's basically an undocumented Matlab wrapper function for JTree, reducing some of the setup overhead. Check another of my FEX submissions, FindJObj, for sample code utilizing and customizing uitree. If you dive into the level of complexity of FindJObj then you might as well use JTree instead, but then again for a simple dialog this is probably overkill and uitree is good enough.

If you're playing around directly with Swing components, remember to always use the EDT to prevent hangs/crashes/exceptions. Do this by using either javacomponent, uicomponent, awtcreate or (in R2008b (7.7) and higher) javaObjectEDT. I suspect the original problem you reported at the top of this thread had to do with this.

Yair Altman
http://undocumented-matlab.com

Subject: Modal JDialog problems

From: Praetorian

Date: 20 Mar, 2009 06:40:40

Message: 6 of 12

On Mar 19, 7:31=A0pm, "Yair Altman" <altmany...@gmailDEL.comDEL> wrote:
> > Thanks for those suggestions Yair; the first one is exactly what I
> > need. I'm still using my class instead of loading uicontrols into the
> > dialog directly because I'm displaying a JTree amongst other things.
> > Thanks again for your help and for posting UICOMPONENT!
>
> If all you need is a JTree, then you could try using Matlab's uitree func=
tion. It's basically an undocumented Matlab wrapper function for JTree, red=
ucing some of the setup overhead. Check another of my FEX submissions, Find=
JObj, for sample code utilizing and customizing uitree. If you dive into th=
e level of complexity of FindJObj then you might as well use JTree instead,=
 but then again for a simple dialog this is probably overkill and uitree is=
 good enough.
>
> If you're playing around directly with Swing components, remember to alwa=
ys use the EDT to prevent hangs/crashes/exceptions. Do this by using either=
 javacomponent, uicomponent, awtcreate or (in R2008b (7.7) and higher) java=
ObjectEDT. =A0I suspect the original problem you reported at the top of thi=
s thread had to do with this.
>
> Yair Altmanhttp://undocumented-matlab.com

Thanks for that tip; I'm new to java programming so excuse me if these
are really simple questions. I'm using R2007b and do not have access
to javaObjectEDT. I did look up the function in the online
documentation.

So is it advisable to use it to create any java class instance? In my
case my class is extending JPanel and all it does is display a bunch
of swing UI objects in a panel. I could've used uitree as you
mentioned but this GUI is related to another one in which I had to
code a JTreeTable and so I'd like to use a JTree for the sake of
uniformity.

Anyway, if I had access to javaObjectEDT should I have used that for
creating an instance of my class? Or any java class for that matter?
Since I don't have javaObjectEDT is there an alternate method to do
this?

Thanks,
Ashish.

Subject: Modal JDialog problems

From: Yair Altman

Date: 20 Mar, 2009 10:05:02

Message: 7 of 12

> Anyway, if I had access to javaObjectEDT should I have used that for
> creating an instance of my class? Or any java class for that matter?
> Since I don't have javaObjectEDT is there an alternate method to do
> this?


As I said above, you can use awtinvoke, javacomponent and uicomponent instead. They also use EDT - javaObjectEDT is simply easier to use, but the end result is the same. You should use EDT for all GUI objects (Swing/AWT components); You don't need EDT for non-GUI objects (java.lang.*, java.util.*, java.net.* etc.).

Yair Altman
http://undocumented-matlab.com
 

Subject: Modal JDialog problems

From: Praetorian

Date: 20 Mar, 2009 16:26:46

Message: 8 of 12

On Mar 20, 4:05=A0am, "Yair Altman" <altmany...@gmailDEL.comDEL> wrote:
> > Anyway, if I had access to javaObjectEDT should I have used that for
> > creating an instance of my class? Or any java class for that matter?
> > Since I don't have javaObjectEDT is there an alternate method to do
> > this?
>
> As I said above, you can use awtinvoke, javacomponent and uicomponent ins=
tead. They also use EDT - javaObjectEDT is simply easier to use, but the en=
d result is the same. You should use EDT for all GUI objects (Swing/AWT com=
ponents); You don't need EDT for non-GUI objects (java.lang.*, java.util.*,=
 java.net.* etc.).
>
> Yair Altmanhttp://undocumented-matlab.com

I'm sorry, I should've worded my question differently. What I meant to
ask was that I wasn't sure about the syntax of using those functions.
I read a little more about the event dispatch thread and I *think* I'm
using uicomponent correctly. Could you please confirm this; here's
what I'm doing.

h =3D myJavaClass( 10 );
%myJavaClass extends JPanel
hDlg =3D dialog( 'Position', [100 100 400 400], 'Visible', 'off' );
uicomponent( h, 'Parent', hDlg, 'Position', [0 0 400 400] );
set( hDlg, 'Visible', 'on' );
uiwait( hDlg );

If I had access to javaObjectEdt I think I'd invoke my class as
follows:

h =3D javaObjectEDT( 'myJavaClass', 10 );
%everything else remains the same

Is that correct? Also, how would I use awtinvoke or javacomponent to
do what I'm doing above?

Thanks again for all your help.
Ashish.

Subject: Modal JDialog problems

From: Yair Altman

Date: 21 Mar, 2009 21:17:01

Message: 9 of 12

> h =3D myJavaClass( 10 );
> %myJavaClass extends JPanel
> hDlg =3D dialog( 'Position', [100 100 400 400], 'Visible', 'off' );
> uicomponent( h, 'Parent', hDlg, 'Position', [0 0 400 400] );
> set( hDlg, 'Visible', 'on' );
> uiwait( hDlg );

This syntax is correct but it bypasses EDT, hence the problem. Instead, use this:
h= awtcreate('myJavaClass',10);

uicomponent doesn't place components on the EDT - Maybe that's something I should fix because I guess most users don't get into the nuts-and-bolts. BTW, javacomponent also doesn't place objects on the EDT if they were pre-created outside the function, as in your case (only when javacomponent is called with a classname).

> If I had access to javaObjectEdt I think I'd invoke my class as
> follows:
>
> h =3D javaObjectEDT( 'myJavaClass', 10 );
> %everything else remains the same
>
> Is that correct?

Yes that's correct

> Also, how would I use awtinvoke or javacomponent to do what I'm doing above?

See above. Also look at the help for awtcreate

Yair Altman
http://Undocumented-Matlab.com
 

Subject: Modal JDialog problems

From: Praetorian

Date: 23 Mar, 2009 19:28:53

Message: 10 of 12

On Mar 21, 3:17=A0pm, "Yair Altman" <altmany...@gmailDEL.comDEL> wrote:
> > h =3D3D myJavaClass( 10 );
> > %myJavaClass extends JPanel
> > hDlg =3D3D dialog( 'Position', [100 100 400 400], 'Visible', 'off' );
> > uicomponent( h, 'Parent', hDlg, 'Position', [0 0 400 400] );
> > set( hDlg, 'Visible', 'on' );
> > uiwait( hDlg );
>
> This syntax is correct but it bypasses EDT, hence the problem. Instead, u=
se this:
> h=3D awtcreate('myJavaClass',10);
>
> uicomponent doesn't place components on the EDT - Maybe that's something =
I should fix because I guess most users don't get into the nuts-and-bolts. =
BTW, javacomponent also doesn't place objects on the EDT if they were pre-c=
reated outside the function, as in your case (only when javacomponent is ca=
lled with a classname).
>
> > If I had access to javaObjectEdt I think I'd invoke my class as
> > follows:
>
> > h =3D3D javaObjectEDT( 'myJavaClass', 10 );
> > %everything else remains the same
>
> > Is that correct?
>
> Yes that's correct
>
> > Also, how would I use awtinvoke or javacomponent to do what I'm doing a=
bove?
>
> See above. Also look at the help for awtcreate
>
> Yair Altmanhttp://Undocumented-Matlab.com

Yair, thanks for pointing me to 'awtcreate', I wasn't even aware of
that function's existence. But I'm still having trouble with the
calling syntax. My class constructor doesn't actually take a simple
integer as it's argument, it's another custom class instance, a
subclass of DefaultMutableTreeNode. I tried several different calling
syntaxes but none of them work. Here's how my code works:

hMyTreeNodeClass =3D myTreeNodeClass( 'root' );
hBranchNode =3D myTreeNodeClass( 'Branch1' );
hMyTreeNodeClass.add( hBranchNode );
%Add some more leaves and branches to root

%The following are the things I tried with awtcreate

h =3D awtcreate( 'myJavaClass', hMyTreeNodeClass );
%This one throws an awtcreate usage error, which makes sense because
the signature of the argument is missing.

h =3D awtcreate( 'myJavaClass', 'LmyTreeNodeClass', hMyTreeNodeClass );
%Throws the error "Illegal signature 'LmyTreeNodeClass'."

h =3D awtcreate( 'myJavaClass',
'Ljavax.swing.tree.DefaultMutableTreeNode', hMyTreeNodeClass );
%Throws the error "Illegal signature
'Ljavax.swing.tree.DefaultMutableTreeNode'."

I'm at a loss at how to make this work; I'd greatly appreciate any
pointers from you. Also, I have another doubt about awtcreate. Let's
say the java class constructor required 2 or more arguments, in that
case how would you handle passing in the signatures? Looking at
awtcreate.m the signature is just the second input argument, not in
the form of param-value pairs.

Thanks for all your help,
Ashish.

Subject: Modal JDialog problems

From: Yair Altman

Date: 24 Mar, 2009 09:09:01

Message: 11 of 12

> h =3D awtcreate( 'myJavaClass', 'LmyTreeNodeClass', hMyTreeNodeClass );
> %Throws the error "Illegal signature 'LmyTreeNodeClass'."
>
> h =3D awtcreate( 'myJavaClass',
> 'Ljavax.swing.tree.DefaultMutableTreeNode', hMyTreeNodeClass );
> %Throws the error "Illegal signature
> 'Ljavax.swing.tree.DefaultMutableTreeNode'."


You forgot the ';' character in your JNI signature:
h = awtcreate( 'myJavaClass', 'LmyTreeNodeClass;', hMyTreeNodeClass );
h = awtcreate( 'myJavaClass','Ljavax.swing.tree.DefaultMutableTreeNode;', ...);

JNI is pretty specific in its demands... Read awtcreate's help, or
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html#wp16432 for details

Yair Altman
http://undocumented-matlab.com
 

Subject: Modal JDialog problems

From: Praetorian

Date: 24 Mar, 2009 15:41:17

Message: 12 of 12

On Mar 24, 3:09=A0am, "Yair Altman" <altmany...@gmailDEL.comDEL> wrote:
> > h =3D3D awtcreate( 'myJavaClass', 'LmyTreeNodeClass', hMyTreeNodeClass =
);
> > %Throws the error "Illegal signature 'LmyTreeNodeClass'."
>
> > h =3D3D awtcreate( 'myJavaClass',
> > 'Ljavax.swing.tree.DefaultMutableTreeNode', hMyTreeNodeClass );
> > %Throws the error "Illegal signature
> > 'Ljavax.swing.tree.DefaultMutableTreeNode'."
>
> You forgot the ';' character in your JNI signature:
> h =3D awtcreate( 'myJavaClass', 'LmyTreeNodeClass;', hMyTreeNodeClass );
> h =3D awtcreate( 'myJavaClass','Ljavax.swing.tree.DefaultMutableTreeNode;=
', ...);
>
> JNI is pretty specific in its demands... Read awtcreate's help, orhttp://=
java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html#wp16432for details
>
> Yair Altmanhttp://undocumented-matlab.com

Pesky little ';', didn't see it at all. Thanks for the Sun
documentation link, that answered all of my questions.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us