简体   繁体   中英

Java block a main with a JDialog

I have a main class in java which call a JFrame Two.

But before call this JFrame Two, my main check a condition and if it's true, it call a JFrame One.

So, my main don't extends JFrame.

I want that my JFrame One stop my main until it been closed, then my main could call my JFrame Two.

I tried to make my JFrame One as a JDialog modal, but my main still running (probably because it isn't a JFrame ?)

Here a simplified part of my code :

File file = new File(PTA);
    if (!file.exists()) {
        FrameOne fo = new FrameOne(); //FrameOne Extends JFrame
    }
    FrameTwo ft = new FrameTwo();

So like you can see, my main will always call FrameTwo.

But i want that until FrameOne was closed, the main class stop running, so it don't call FrameTwo until FrameOne was closed.

I don't really know how a JDialog works, i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.

Please can you help me ? My project is blocked by this problem...

Thanks for your attention and your help.

Regards,

Maxime OZENNE.

i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.

It doesn't work that way, you and you can't make a JFrame modal this way (the JFrame doesn't even have this method). Instead you're going to have to use a JDialog and not a JFrame, and create the dialog so that it's modal. Myself I avoid creating classes that extend JFrame or the like and instead gear my code towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

So on that note, I'd do something on these lines:

MyPanel myPanel = new MyPanel();

// assuming that the parent window JFrame is referenced by "myFrame"
JDialog myDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
myDialog.add(myPanel);
myDialog.pack();
myDialog.setVisible(true);

If this doesn't help, then please create and post your Minimal, Complete, and Verifiable example program, and let us help modify it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM