javaswingnetbeansswingxjform

Closing Jform in the constructor


I am having a minor issue with the code. I just want the Jform to not be displayed if a condition is not met in the constructor portion of the form.Outside the constructor the dispose(), return and setVisible(false) all work fine. I have tried this.dispose(); and return; and this.setVisible(false); but the form is still displayed. With System.exit(0); it closes the complete app. Would really appreciate if someone can help me with this.

public class OrderGUI extends javax.swing.JFrame {

public OrderGUI(Customer cust, Date dt, Time t) throws FileNotFoundException, IOException, ClassNotFoundException {
    this();
if(condition)
{
/////do not initialize the Jform
}else{//// run rest of the code}
}

Solution

  • do something like this

    public class OrderGUI extends javax.swing.JFrame {
        public OrderGUI(Customer cust, Date dt, Time t) throws FileNotFoundException, IOException, ClassNotFoundException {
           this();
        }
    
       @Override
       public void setVisible(boolean val){
           if(!condition){
               super.setVisible(val);
           } 
       }
    }