JPanel like JDialog

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aleplgr
    New Member
    • Aug 2007
    • 19

    JPanel like JDialog

    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.

    Code:
    jmnuTest.addActionListener(
             new ActionListener()
             {
                public void actionPerformed(ActionEvent e)
                {
                   jmnuTest_actionPerformed(e);
                }
             });

    Code:
    private void jmnuTest_actionPerformed(ActionEvent e)
       {
          Testwhen dlg = new Testwhen(this);
         System.out.println("I'm here");
          
       }


    And I need the JPanel to behave like the JDialog in that sense.. how could I deal with this?
    Thanks in advance
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by aleplgr
    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.

    [CODE=java]jmnuTest.addAct ionListener(
    new ActionListener( )
    {
    public void actionPerformed (ActionEvent e)
    {
    jmnuTest_action Performed(e);
    }
    });[/CODE]


    [CODE=java]private void jmnuTest_action Performed(Actio nEvent e)
    {
    Testwhen dlg = new Testwhen(this);
    System.out.prin tln("I'm here");

    }[/CODE]



    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.
    [CODE=java]dlg.setModal(fa lse);[/CODE]

    Comment

    • aleplgr
      New Member
      • Aug 2007
      • 19

      #3
      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(tr ue) is not defined when dlg is a JPanel...

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by aleplgr
        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(tr ue) 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.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          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

          Comment

          • aleplgr
            New Member
            • Aug 2007
            • 19

            #6
            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?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by aleplgr
              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

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by aleplgr
                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.

                Comment

                • aleplgr
                  New Member
                  • Aug 2007
                  • 19

                  #9
                  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...

                  Code:
                  private JPanel contentPane;
                   
                  dlg = new MyDialog(this);
                  pnl =  new MyPanel (this);
                  dlg.setContentPane(pnl);
                  contentPane.add(dlg,  BorderLayout.CENTER);

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by aleplgr
                    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...

                    Code:
                    private JPanel contentPane;
                     
                    dlg = new MyDialog(this);
                    pnl =  new MyPanel (this);
                    dlg.setContentPane(pnl);
                    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

                    Comment

                    Working...