简体   繁体   中英

JFrame "blocking" show()

I'm using CHARVA (ncurses-like Swing) and I have a problem.

Because charva is very similar to Swing, I think the solution for Swing is the same to charva.

I'm developing an application that call a JFrame -extended class, get its return code and shows other JFrame windows depending on return code.

It's like :

public static void main() {
MainFrame mainframe=new MainFrame();
mainframe.show();
switch (mainframe.returncode) {
       case "generalsetting": Frame1 frame1=new Frame1;
       frame1.show();
       break;
       case "usersetting": Frame2 frame2=new Frame2;
       frame2.show();
       break;

etc. But when I do:

mainframe.show();

The program does not wait until mainframe is closed/hidden, but continue on the "switch", the return code is not initialized and crashes my program.

There is a way to have a "blocking" show() ?

If you want to perform a blocking call, you can try using a modal JDialog instead of a JFrame : simply use true for modal parameter in its constructor (see JDialog )

Although, I don't know Charva, but JFrame.show() is deprecated since Java 1.5 (you shall use setVisible(true) instead).

there is a way to have a "blocking" show()?

Yes. You can make MainFrame extend JDialog instead of JFrame and make it modal:

class MainFrame extends JDialog {
    //code...
}

System.out.println("Before");
MainFrame d = new JDialog();
d.setModal(true);
d.setVisible(true);
System.out.println("After");

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