0

So here's the issue. I'm creating a program that uses JFrame. Essentially what it does is opens a window that asks you which window you then want to open. This program is comprised of multiple GUI classes, and multiple Client classes in charge of creating a new window for each GUI when it's opened. So when the MainClient class is loaded, it creates a window to hold the MainGUI class. From there selecting an option from the ComboBox and clicking "continue" should initiate another client class opening another JFrame. This all works fine, except i can't for the life of me figure out why i can't use the .dispose() method to remove the old window. All i'm able to do is use the setVisible(false) method to clear it off, but then it still hangs around in the background of my new window. Whenever i try to use the dispose method directly after the setVisible() method i get this error "Cannot find symbol - method dispose()" If anyone knows why this is happening i'd love some help on this! Here's the code for my MainGUI class that causes the error (The error is near the bottom commented out and surrounded by asterisks):

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 


public class MainGUI extends JPanel
{

  protected JLabel topLabel;
  protected JButton        continueButton;// To continue to the next form
  private MainHandler handler = new MainHandler(this); // An instance of the inner event handler
  protected final String[] OPTIONS = {"Search/Delete Employee Records","Insert New Employee",
  "Insert New Manager","Retrieve Department Metadata"};
  protected JComboBox       optionsCombo; //Combo box to select options from

  protected InsManGUI insGUI;

  public MainGUI ()
  {
      setLayout (new FlowLayout());

      topLabel = new JLabel("Please select which action you wish to perform");
      topLabel.setHorizontalAlignment(JLabel.CENTER);

      continueButton = new JButton("Continue");
      continueButton.setHorizontalAlignment(JLabel.CENTER);

      optionsCombo = new JComboBox(OPTIONS);


      add(topLabel);

      add(optionsCombo);

      add(continueButton);

      continueButton.addActionListener(new MainHandler(this));
      optionsCombo.addActionListener(new MainHandler(this));







  }    
}

// Inner eventhandler class to compute which form to open once the Continue button is clicked.
class MainHandler implements ActionListener
{
//Private varible to hold an instance of the MainGUI class
private MainGUI gui;




 public MainHandler(MainGUI gui)
   {
       //Set the private GUI varible to a particular GUI
   this.gui = gui;

   EmployeesDB.connect();

   }  


    public void actionPerformed(ActionEvent e)
   {
   if(e.getSource() == gui.continueButton)
   { 
       if(gui.optionsCombo.getSelectedItem() == "Insert New Manager")
       {
           gui.setVisible(false);

           InsManClient man = new InsManClient();

           //gui.dispose();*******************THIS LINE WON't //COMPILE*************************


       }

       if(gui.optionsCombo.getSelectedItem() == "Insert New Employee")
       {
           gui.setVisible(false);
           InsEmpClient emp = new InsEmpClient();
       }    

       if(gui.optionsCombo.getSelectedItem() == "Search/Delete Employee Records")
       {
           gui.setVisible(false);
           SearchClient ser = new SearchClient();
       }  

       if(gui.optionsCombo.getSelectedItem() == "Retrieve Department     Metadata")
       {
           gui.setVisible(false);
           MetaDataClient met = new MetaDataClient();
       }  


   }
   }
}
Stan Harris
  • 15
  • 1
  • 4
  • `gui` is a `JPanel`, not a `JFrame`, and `JPanel` does not have a `dispose()` method. [`JPanel` documentation](https://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html). – Jonny Henly Nov 13 '16 at 22:00

1 Answers1

0

Your MainGUI class extends JPanel, not JFrame. If you wish to dispose the frame holding the instance of the MainGUI panel, you can use:

((JFrame) SwingUtilities.getWindowAncestor(gui)).dispose();

It also seems like you are using more than one JFrame, see The Use of Multiple JFrames: Good or Bad Practice?

Community
  • 1
  • 1
MasterBlaster
  • 948
  • 2
  • 12
  • 26