469,271 Members | 1,000 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

JPanel like JDialog

19
Hello, in this code suppose that the dlg is defined in one case as a JDialog, I run it and it seems that the JDialog is executed AFTER closing it, so the message is printed after closing the JDialog.
But when I run the code with dlg defined as JPanel it prints the message before I close the JPanel.

Expand|Select|Wrap|Line Numbers
  1. jmnuTest.addActionListener(
  2.          new ActionListener()
  3.          {
  4.             public void actionPerformed(ActionEvent e)
  5.             {
  6.                jmnuTest_actionPerformed(e);
  7.             }
  8.          });

Expand|Select|Wrap|Line Numbers
  1. private void jmnuTest_actionPerformed(ActionEvent e)
  2.    {
  3.       Testwhen dlg = new Testwhen(this);
  4.      System.out.println("I'm here");
  5.  
  6.    }


And I need the JPanel to behave like the JDialog in that sense.. how could I deal with this?
Thanks in advance
Aug 30 '07 #1
9 10676
Nepomuk
3,112 Expert 2GB
Hello, in this code suppose that the dlg is defined in one case as a JDialog, I run it and it seems that the JDialog is executed AFTER closing it, so the message is printed after closing the JDialog.
But when I run the code with dlg defined as JPanel it prints the message before I close the JPanel.

Expand|Select|Wrap|Line Numbers
  1. jmnuTest.addActionListener(
  2.          new ActionListener()
  3.          {
  4.             public void actionPerformed(ActionEvent e)
  5.             {
  6.                jmnuTest_actionPerformed(e);
  7.             }
  8.          });

Expand|Select|Wrap|Line Numbers
  1. private void jmnuTest_actionPerformed(ActionEvent e)
  2.    {
  3.       Testwhen dlg = new Testwhen(this);
  4.      System.out.println("I'm here");
  5.  
  6.    }


And I need the JPanel to behave like the JDialog in that sense.. how could I deal with this?
Thanks in advance
Is your JDialog modal? If so, that would explain the behavior. It will not continue doing anything as long as the JDialog is visible - you will have to switch it to nonmodal.
Expand|Select|Wrap|Line Numbers
  1. dlg.setModal(false);
Aug 30 '07 #2
aleplgr
19
Thanks, then as I understand I need the JPanel to be modal, the JDialog is modal and that's why it shows the message after closing it. The JPanel is not modal and that's why it does not wait until I close it to print the message.
So as I understand I should have to make the JPanel modal, but dlg.setModal(true) is not defined when dlg is a JPanel...
Aug 30 '07 #3
Nepomuk
3,112 Expert 2GB
Thanks, then as I understand I need the JPanel to be modal, the JDialog is modal and that's why it shows the message after closing it. The JPanel is not modal and that's why it does not wait until I close it to print the message.
So as I understand I should have to make the JPanel modal, but dlg.setModal(true) is not defined when dlg is a JPanel...
No, you misunderstood.
JPanels can't be modal. Modal means, that it is in the foreground and nothing else can be accessed. If you want the JDialog to act like the JPanel, you have to switch the JDialog to nonmodal. If you want a JPanel to act like it was modal, you'll have to write that yourself.
Aug 30 '07 #4
JosAH
11,448 Expert 8TB
Note that since Java 1.5. (or 1.6, I don't remember) there are more levels of
modality to pick from than just (non)modal; check the JDialog API documenation
for the details.

kind regards,

Jos
Aug 30 '07 #5
aleplgr
19
But the JPanel prints the message "I'm here" before closing it
and the JDialog prints the message after closing it.
I need the JPanel to print the message after closing it, how could I do that?
Aug 30 '07 #6
JosAH
11,448 Expert 8TB
But the JPanel prints the message "I'm here" before closing it
and the JDialog prints the message after closing it.
I need the JPanel to print the message after closing it, how could I do that?
How do you 'close' a JPanel? Al you can do with it is remove it from its container,
but that's about it. If you want some modality in your application, i.e. a use is
only allowed to work with a particular piece of your appliation, use a modal dialog.
A JDialog is a top level container (like a window) while a JPanel is just part of
another (possibly top level) container.

kind regards,

Jos
Aug 30 '07 #7
Nepomuk
3,112 Expert 2GB
But the JPanel prints the message "I'm here" before closing it
and the JDialog prints the message after closing it.
I need the JPanel to print the message after closing it, how could I do that?
In that case, you should add a listener to the JPanel which is activated, when the Panel is closed (e.g. made invisible). You could have a while-loop, which stops the program from doing anything else until it gets some kind of message from the listener.
Aug 31 '07 #8
aleplgr
19
I've been thinking the first solution: to define it as a JDialog.
And now it works fine, but I need to add everything else (buttons, labels, functionality) from the JPanel to the JDialog.
I tried the setContentPane to "copy" everything from the JPanel to the JDialog but it's not working...It shows the JDialog as if I had done nothing...

Expand|Select|Wrap|Line Numbers
  1. private JPanel contentPane;
  2.  
  3. dlg = new MyDialog(this);
  4. pnl =  new MyPanel (this);
  5. dlg.setContentPane(pnl);
  6. contentPane.add(dlg,  BorderLayout.CENTER);
Aug 31 '07 #9
JosAH
11,448 Expert 8TB
I've been thinking the first solution: to define it as a JDialog.
And now it works fine, but I need to add everything else (buttons, labels, functionality) from the JPanel to the JDialog.
I tried the setContentPane to "copy" everything from the JPanel to the JDialog but it's not working...It shows the JDialog as if I had done nothing...

Expand|Select|Wrap|Line Numbers
  1. private JPanel contentPane;
  2.  
  3. dlg = new MyDialog(this);
  4. pnl =  new MyPanel (this);
  5. dlg.setContentPane(pnl);
  6. contentPane.add(dlg,  BorderLayout.CENTER);
It doesn't work like that; forget about that JPanel and add all the components to
the content pane of the JDialog instead. Don't be afraid to throw away incorrect
code and don't try to hack it a bit more; change it so that everything is correct.

kind regards,

Jos
Aug 31 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Seanus | last post: by
1 post views Thread by dishal | last post: by
5 posts views Thread by James Barrett | last post: by
1 post views Thread by rpm27 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.