Osiris93
Osiris93

Reputation: 189

Dispose method to close a JFrame (but not the entire program) from a separate class in NetBeans is not working. Why?

I have three classes I am working with here. The first is an admin page where the user can select to either add, update or delete an employee on the system via a drop down box. When the user selects one of the three options from the combo box, a JFrame (employees) comes up with the necessary fields to perform their task and the admin frame will still be displayed behind it. On this frame there is a "Cancel" button. When they click the cancel button only this frame must close but keep the admin frame open still. The button is generated from a separate class (empClass) to be displayed on the employee frame. My problem now is I am struggling to get the button to dispose of the employee frame, but out of the several ways of trying this it cannot work. One way produced an error each time I ran the application, another method caused the application to crash/freeze whenever I try select an option to perform on the employee frame and the code I have currently implemented performs no action at all. I think the problem is a an issue with communicating with the forms but I am not entirely sure. Please help as I have been struggling with this for hours and the internet is providing absolutely nothing useful. Most resources refer to the dispose() method which I have tried in various ways but all the ways I have tried do not work, crash the application or cause errors to occur. Even the other questions similar to this on here have not helped me out at all.

I have tried calling the button from the employee frame to try and link the function to the "Cancel" button. Here is the code I have implemented in the empClass:

public void disposeof()
    {

        employees empp = new employees();
        empp.dispose();
    }

private void cancelActionPerformed(java.awt.event.ActionEvent evt)
    {
        disposeof();
    }

Here is the employee coding:

public class employees extends javax.swing.JFrame {

    empClass ec = new empClass();
    adminPage ap = new adminPage();

    public employees() {
        initComponents();
      getContentPane().add(ec.getpanel());
       this.add(ec.getpanel());
       this.add(ec.lbltitle);
       this.add(ec.cancel);
       this.add(ec.bfunction);
       this.add(ec.empList); 
    }

Upvotes: 0

Views: 1248

Answers (3)

Freak
Freak

Reputation: 6883

As you didn't provide the code of your JFrame so I guess problem is in the code of your JFrame. You might be setting setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE) for your employee class as it is static property so it will close all JFrames. You should set it setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE) OR setDefaultCloseOperation(employees.HIDE_ON_CLOSE).
And after that while triggering your event you can call empp.dispose(); OR setVisible(flase).

Upvotes: 2

Java Apprentice
Java Apprentice

Reputation: 92

Another way you can do this is to hide the frame using setVisible(false).

Upvotes: 0

Christophe Schutz
Christophe Schutz

Reputation: 613

First of all only use one JFrame and use JDialogs for the underlying other windows you may want to see appearing. On JDialog, use setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE).

Upvotes: 1

Related Questions