1

I had another question here and it was suggested me to make the program "modal" in order to block the program from running to the next line and "pausing" the program when a popup happened. I have tried it, but I do not understand how it works 100%.

Here's my code I tried to test with:

import javax.swing.*;
import java.awt.*;

public class test {

    public static void main(String[] args) {
        JFrame theDog = new JFrame();
        theDog.setSize(200,200);
        theDog.setVisible(true);
        new JDialog(theDog,"theTitle", Dialog.ModalityType.APPLICATION_MODAL);

        System.out.println("hello");
    }
}

When the JPanel box pops up, it should "halt" the program until an action is taken there. For example, I want to have a button push based on a picture. I'm looking specifically to do this with a custom JPanel/JFrame, not using some JOptionPane get input dialog or anything like that.

When I run the code above, the system.out.println runs no matter what, even if I haven't closed the dialog box, it's like the program ignores the pop up. How can I halt and wait for a user action before taking the next move?

Here's the previous question I had asked with suggestions given: How Do You Halt The Current Programs "Progress" at A JPanel/JFrame Pop Up?

Community
  • 1
  • 1
king
  • 1,304
  • 3
  • 23
  • 43

1 Answers1

2

You need to make the dialog visible before it will block...

JDialog dialog = new JDialog(theDog,"theTitle", Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible();

Windows in Swing are not visible by default

Have a look at How to Make Dialogs for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • feels like inception.. so many things inside things with Jpanel, Jframe, Jdialog now.. – king Apr 29 '15 at 06:38
  • Yep. But remember, `JDialog` and `JFrame` should be used to display other components, this way, you can re-use components in different contexts as well as produce complex UI's by combining components within a window...but that's me ;) – MadProgrammer Apr 29 '15 at 06:39
  • I don't understand, can you say that in another way? What do you mean to "display other components"? You mean, place buttons, labels, pictures, etc. inside of a JFrame or a JDialog, and the entire "window" should be a JPanel? Should JDialog be used to display everything it seems like, how else am I supposed to stop the program from running without it there? – king Apr 29 '15 at 06:41
  • 1
    What I mean is, resist the urge to extend from `JFrame` or `JDialog` to create your base UI's, use a `JPanel` as the primary container and build complexity through the use of diverse chunks, rather then simply trying to put all your eggs in a single basket. Create small, logical groups of functionality, we might be re-used or used differently at a different time. It de-couples your code and makes it more flexible ;) – MadProgrammer Apr 29 '15 at 06:45
  • I understand fundamentally what you are saying. However, where I am confused is the fact that I need a JDialog box in order to "stop" the program from continuing from running. It seems to me that Jpanel or JFrame do not have these functions, correct? Does only one "component" or "section" have to be JDialog in order for the modality to run or does it have to be the entire viewing layer? I noticed on my example above, I had two popups because I had a JFrame setVisible to true and a JDialog box set to true. So it seems I should only have the JDialogBox? Do you know what I'm saying ish? – king Apr 29 '15 at 06:52
  • Use, `JDialog` is the correct tool in this case, but what you display on the dialog should be separate component, with it's own contained logic. You don't "have" to have a `JFrame` (or other window) to display a `JDialog`, but generally speaking, a `JDialog` is used to prompt the user for some kind of action, which, until they respond to the dialog, the rest of the application can't continue, a bit like a login dialog. Although there are ways to do this without a dialog, it's typically preferred in most cases – MadProgrammer Apr 29 '15 at 06:55
  • Okay I understand what you are saying. In this scenario, do you recommend I use a JDialog box as the container, and then a JFrame for each other component or a JPanel for each other component? They seem very similar to me. It should be JPanel correct? – king Apr 29 '15 at 06:59
  • For example, I used a JFrame above.. even though it's for windows, it still gave the same illusion as the correct way to do it? You know what I mean? – king Apr 29 '15 at 07:00
  • I recommend using a `JPanel` as you "base" container for your core views and the use `JDialog` or `JFrame` to actually show these on the screen. [This](http://stackoverflow.com/questions/26517856/java-swing-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) is a little more complicated then what you are doing, but presents the basic idea – MadProgrammer Apr 29 '15 at 07:02
  • Thank you. I bookmarked the other question for reference, but it does look fairly advanced for me at this time. Thanks for all your help. Have a good night. – king Apr 29 '15 at 07:07