0

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); 
    }
Osiris93
  • 189
  • 1
  • 15
  • possible duplicate of [Close one JFrame without closing another?](http://stackoverflow.com/questions/1944446/close-one-jframe-without-closing-another) – Freak Jul 07 '15 at 15:50
  • did you add `setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE)`? or tried `setVisible(flase)` – Freak Jul 07 '15 at 15:55
  • then you should set all properties of jframe by making an object of employees and then pass this employees object through the method `disposeof` and then call `dispose()` by passed object OR dispose it by keyword `this` – Freak Jul 07 '15 at 16:27
  • I am unable to understand this part `empClass ec = new empClass(); adminPage ap = new adminPage();`. what is `empClass` and `adminPage`.Trust me there is nothing wrong with `JFrame` , problem is in your OOP implementation. – Freak Jul 07 '15 at 16:57
  • The `adminPage` is a Swing Frame where the user will access the employees Frame. `empClass` is the java class where the objects such as buttons and labels etc. are created to be displayed on the employees frame. I just deleted the `adminPage ap = new adminPage();` as that was from a previous attempt I tried that is no longer relevant. The `employees ec = new employees();` is what we used to get the objects from the `empClass` to display on the employees Frame. – Osiris93 Jul 07 '15 at 17:04

3 Answers3

2

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).

Freak
  • 6,786
  • 5
  • 36
  • 54
  • I just tried that in this manner: `public void disposeof() { employees empp = new employees(); empp.setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE); empp.dispose(); } private void cancelActionPerformed(java.awt.event.ActionEvent evt) { disposeof(); }` It still is not working. Did I implement it in the correct way? – Osiris93 Jul 07 '15 at 16:23
  • so is it still not working? have you tried `setVisible(false)`? – Freak Jul 07 '15 at 16:24
  • `setVisible(false)` also isn't working :( But is what I've done right? Should I maybe edit my question and include ALL the coding in in case it's something elsewhere in the coding causing the problem that I'm not seeing? – Osiris93 Jul 07 '15 at 16:45
  • I included the employees coding in an edit on the question just now – Osiris93 Jul 07 '15 at 16:49
  • and your `AdminJframe` is another class extended by JFrame? it should be? isn't it? – Freak Jul 07 '15 at 17:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82631/discussion-between-freak-and-osiris93). – Freak Jul 07 '15 at 17:12
1

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).

0

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