简体   繁体   中英

How can I show a JDialog at the start of my main JFrame form?

Good evening,

I have a simple Java application I'm working on. I've gotten stuck and I need some assistance. This is a project we were working on in my Java Programming class.

I've created a JFrame form and several JDialog forms that I calling successfully using buttons located on the JFrame form. For one of the JDialog forms ( dlg_create_company ), I'd like that to popup immediately as a modal window when the application starts. So far I've tried calling the code from the main method of the JFrame form, but I run into the "can't call a non-static variable in a static context" error.

Here's what I'm trying:

    //Create a new instance of my JDialog "dlg_create_company" and assign it to "dialog"
    //Then set it's visibility to true.
    dlg_create_company dialog = new dlg_create_company(new javax.swing.JFrame(), true, current_company);
    dialog.my_parent = this; //sets form frm_repair_shop as the parent via the my_parent reference.
    dialog.setVisible(true);

Any assistance would be greatly appreciated. Here's a link to a zip of my entire project.

Of course, you cannot access a static variable in non-static context. That is because static variables 'exist all the time' while non static objects may or may not 'exist'. You need to show the dialog box in the constructor of your JFrame and make both of them visible. The JFrame first and the JDialog after.

And please, post an SSCCE instead of linking to the ZIP file. Maybe tomorrow people will start linking to their Github asking us to debug it.

public MyJFrame(){
    // add components
    // call pack and stuff
    // instantiate the JDialog
    // make JFrame visible
    // make the JDialog visible
}

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